From 136d7cc280a40e97c0e214b6fa0ce7be0490de6c Mon Sep 17 00:00:00 2001 From: jiannei Date: Sat, 20 Mar 2021 12:53:09 +0800 Subject: [PATCH] feat: Add config for fail http status #19 --- config/response.php | 34 +++++++++++++++++++++++++++++++++- src/Response.php | 2 +- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/config/response.php b/config/response.php index 9f3dd76..023183a 100644 --- a/config/response.php +++ b/config/response.php @@ -12,10 +12,42 @@ return [ 'enum' => \Jiannei\Enum\Laravel\Repositories\Enums\HttpStatusCodeEnum::class, - 'error_code' => \Jiannei\Enum\Laravel\Repositories\Enums\HttpStatusCodeEnum::HTTP_INTERNAL_SERVER_ERROR, + /* + |-------------------------------------------------------------------------- + | Set the http status code when the response fails + |-------------------------------------------------------------------------- + | + | the reference options are false, 200, 500 + | + | false, stricter http status codes such as 404, 401, 403, 500, etc. will be returned + | 200, All failed responses will also return a 200 status code + | 500, All failed responses return a 500 status code + */ + + 'error_code' => false, + + // Set the http status code returned when the form validation fails. + // When the error_code is set to 200 or 500, it will not work 'validation_error_code' => \Jiannei\Enum\Laravel\Repositories\Enums\HttpStatusCodeEnum::HTTP_UNPROCESSABLE_ENTITY, + // Set the structure of the paging data return,the following structure will be returned by default, + // You can modify the name of the inner data field through the following configuration items, such as rows or list + //{ + // "status": "success", + // "code": 200, + // "message": "Success.", + // "data": { + // "data": [ + // // ... + // ], + // "meta": { + // // ... + // } + // }, + // "error": {} + //} + 'format' => [ 'paginated_resource' => [ 'data_field' => 'data', diff --git a/src/Response.php b/src/Response.php index 7994c76..3535deb 100644 --- a/src/Response.php +++ b/src/Response.php @@ -145,7 +145,7 @@ public function fail(string $message = '', int $code = 500, $errors = null, arra { $response = $this->response( $this->formatData(null, $message, $code, $errors), - Config::get('response.error_code', $code), + Config::get('response.error_code') ?: $code, $header, $options );