Skip to content

Commit

Permalink
Adding a custom error response.
Browse files Browse the repository at this point in the history
  • Loading branch information
joshforbes committed Apr 12, 2016
1 parent 4bdaa55 commit 0ee51e5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 14 deletions.
28 changes: 19 additions & 9 deletions src/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ protected function respondWithNoContent()
*/
protected function errorForbidden($message = 'Forbidden')
{
return $this->setStatusCode(403)->respondWithError($message, self::CODE_FORBIDDEN);
return $this->setStatusCode(403)->respondWithError($message);
}

/**
Expand All @@ -206,7 +206,7 @@ protected function errorForbidden($message = 'Forbidden')
*/
protected function errorInternalError($message = 'Internal Error')
{
return $this->setStatusCode(500)->respondWithError($message, self::CODE_INTERNAL_ERROR);
return $this->setStatusCode(500)->respondWithError($message);
}

/**
Expand All @@ -217,7 +217,7 @@ protected function errorInternalError($message = 'Internal Error')
*/
protected function errorNotFound($message = 'Resource Not Found')
{
return $this->setStatusCode(404)->respondWithError($message, self::CODE_NOT_FOUND);
return $this->setStatusCode(404)->respondWithError($message);
}

/**
Expand All @@ -228,7 +228,7 @@ protected function errorNotFound($message = 'Resource Not Found')
*/
protected function errorUnauthorized($message = 'Unauthorized')
{
return $this->setStatusCode(401)->respondWithError($message, self::CODE_UNAUTHORIZED);
return $this->setStatusCode(401)->respondWithError($message);
}

/**
Expand All @@ -239,7 +239,7 @@ protected function errorUnauthorized($message = 'Unauthorized')
*/
protected function errorUnprocessableEntity($message = 'Unprocessable Entity')
{
return $this->setStatusCode(422)->respondWithError($message, self::CODE_UNPROCESSABLE_ENTITY);
return $this->setStatusCode(422)->respondWithError($message);
}

/**
Expand All @@ -250,7 +250,19 @@ protected function errorUnprocessableEntity($message = 'Unprocessable Entity')
*/
protected function errorWrongArgs($message = 'Wrong Arguments')
{
return $this->setStatusCode(400)->respondWithError($message, self::CODE_WRONG_ARGS);
return $this->setStatusCode(400)->respondWithError($message);
}

/**
* Returns a response that indicates custom error type.
*
* @param $message
* @param $statusCode
* @return \Illuminate\Http\JsonResponse
*/
protected function errorCustomType($message, $statusCode = 400)
{
return $this->setStatusCode($statusCode)->respondWithError($message);
}

/**
Expand All @@ -268,14 +280,12 @@ protected function errorArray(array $errors)
* Returns a response that indicates an an error occurred.
*
* @param $message
* @param $errorCode
* @return \Illuminate\Http\JsonResponse
*/
private function respondWithError($message, $errorCode)
private function respondWithError($message)
{
return $this->respondWithArray([
'errors' => [
'code' => $errorCode,
'http_code' => $this->statusCode,
'message' => $message,
]
Expand Down
27 changes: 22 additions & 5 deletions tests/Integration/ControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,6 @@ public function it_can_respond_with_error_forbidden()

$expectedArray = [
'errors' => [
'code' => 'GEN-FORBIDDEN',
'http_code' => 403,
'message' => 'Forbidden'
]
Expand All @@ -254,7 +253,6 @@ public function it_can_respond_with_an_internal_error()

$expectedArray = [
'errors' => [
'code' => 'GEN-INTERNAL-ERROR',
'http_code' => 500,
'message' => 'Internal Error'
]
Expand All @@ -277,7 +275,6 @@ public function it_can_respond_with_a_not_found_error()

$expectedArray = [
'errors' => [
'code' => 'GEN-NOT-FOUND',
'http_code' => 404,
'message' => 'Resource Not Found'
]
Expand All @@ -300,7 +297,6 @@ public function it_can_respond_with_a_unauthorized_error()

$expectedArray = [
'errors' => [
'code' => 'GEN-UNAUTHORIZED',
'http_code' => 401,
'message' => 'Unauthorized'
]
Expand All @@ -323,7 +319,6 @@ public function it_can_respond_with_an_unprocessable_entity()

$expectedArray = [
'errors' => [
'code' => 'GEN-UNPROCESSABLE-ENTITY',
'http_code' => 422,
'message' => 'Unprocessable Entity'
]
Expand Down Expand Up @@ -355,6 +350,28 @@ public function it_can_respond_with_a_wrong_arguments_error()
$this->assertEquals('400', $response->status());
}

/**
* @test
*/
public function it_can_respond_with_a_custom_error()
{
$errorCustomType = $this->getMethod('errorCustomType');
$testController = new TestController();

$response = $errorCustomType->invokeArgs($testController, ['Test error', 402]);
$array = json_decode(json_encode($response->getData()), true);

$expectedArray = [
'errors' => [
'http_code' => 402,
'message' => 'Test error'
]
];

$this->assertEquals($expectedArray, $array);
$this->assertEquals('402', $response->status());
}

/**
* @test
*/
Expand Down

0 comments on commit 0ee51e5

Please sign in to comment.