| @@ -1,7 +1,10 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | namespace Depicter\Utility; |
| 3 | 3 | |
| 4 | +use Psr\Http\Message\ResponseInterface; | |
| 5 | + | |
| 6 | + | |
| 4 | 7 | class Http { |
| 5 | 8 | |
| 6 | 9 | public static function getErrorExceptionResponse( \Exception $exception ){ |
| 7 | 10 | $isConnectionError = method_exists( $exception, 'getHandlerContext' ); |
| @@ -30,8 +33,27 @@ | ||
| 30 | 33 | } |
| 31 | 34 | if( ! empty( $error['context']['errno'] ) ){ |
| 32 | 35 | $error['errCode'] = $error['context']['errno']; |
| 33 | 36 | } |
| 37 | + if( $error['errCode'] == 401 ){ | |
| 38 | + $error['message'] = "You are not authorized to access this resource."; | |
| 39 | + } | |
| 34 | 40 | |
| 35 | 41 | return $error; |
| 42 | + } | |
| 43 | + | |
| 44 | + /** | |
| 45 | + * Generate a Json response based on errors variable | |
| 46 | + * | |
| 47 | + * @param $errors | |
| 48 | + * | |
| 49 | + * @return ResponseInterface | |
| 50 | + */ | |
| 51 | + public static function getErrorJson( $errors ){ | |
| 52 | + $errors = (array) $errors; | |
| 53 | + $statusCode = (int) ( !empty( $errors['code'] ) ? $errors['code'] : 200 ); | |
| 54 | + | |
| 55 | + return \Depicter::json([ | |
| 56 | + 'errors' => $errors | |
| 57 | + ])->withStatus( max( $statusCode, 200 ) ); | |
| 36 | 58 | } |
| 37 | 59 | } |