Skip to content

Commit

Permalink
Move default HTTP response status (i.e. 200) to context class so it c…
Browse files Browse the repository at this point in the history
…an be modified at runtile
  • Loading branch information
jjrom committed Dec 7, 2023
1 parent 47a116a commit b9829e7
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 5 deletions.
10 changes: 9 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,12 @@ saml/cert/server.pem
resto-model
tmp
pgdata
pgdata.old
pgdata.old
app/resto/addons
errors.log
.private
.DS_Store
.php-cs-fixer.cache
# Local config and attached docker-compose
_config*.env
_docker-compose*.yml
3 changes: 2 additions & 1 deletion app/resto/core/Resto.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ public function __construct($config = array())
* Process route
*/
$response = $this->getResponse();

} catch (Exception $e) {
/*
* Output in error - format output as JSON in the following
Expand All @@ -165,7 +166,7 @@ public function __construct($config = array())
$response = json_encode(array('ErrorMessage' => $e->getMessage(), 'ErrorCode' => $e->getCode()), JSON_UNESCAPED_SLASHES);
}

$this->answer($response ?? null, $responseStatus ?? 200);
$this->answer($response ?? null, $responseStatus ?? $this->context->httpStatus);
}

/**
Expand Down
5 changes: 5 additions & 0 deletions app/resto/core/RestoContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ class RestoContext
*/
public $outputFormat = 'json';

/**
* Default response HTTP status
*/
public $httpStatus = 200;

/**
* Path
*/
Expand Down
21 changes: 21 additions & 0 deletions app/resto/core/utils/RestoLogUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@ class RestoLogUtil
*/
public static $codes = array(
200 => 'OK',
201 => 'Created',
206 => 'Partial Content',
400 => 'Bad Request',
401 => 'Unauthorized',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
409 => 'Conflict',
410 => 'Gone',
412 => 'Precondition Failed',
500 => 'Internal Server Error'
);
Expand Down Expand Up @@ -158,6 +160,25 @@ class RestoLogUtil
* "ErrorMessage":"Not Found"
* }
* )
*
* @OA\Schema(
* schema="GoneError",
* required={"ErrorCode", "ErrorMessage"},
* @OA\Property(
* property="ErrorCode",
* type="integer",
* description="HTTP status code"
* ),
* @OA\Property(
* property="ErrorMessage",
* type="string",
* description="Error message"
* ),
* example={
* "ErrorCode":410,
* "ErrorMessage":"Gone"
* }
* )
*/
public static function httpError($code, $message = null)
{
Expand Down
3 changes: 0 additions & 3 deletions build/resto/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ RUN mkdir /var/www/static
# Copy template configuration file to /tmp (will be used by 15-resto-config.sh during startup to generate /etc/resto/config.php)
COPY ${BUILD_DIR}/config.php.template /tmp/config.php.template

# Create configuration directory
RUN mkdir /cfg

# Docs directory (for API)
RUN mkdir -p /docs
COPY ./docs /docs

0 comments on commit b9829e7

Please sign in to comment.