Skip to content

Commit

Permalink
Refactor JSON error response and log error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Caiger committed May 24, 2017
1 parent d084f43 commit f96ae98
Showing 1 changed file with 18 additions and 21 deletions.
39 changes: 18 additions & 21 deletions Helper/ApiFunctions.php
Expand Up @@ -68,11 +68,7 @@ public function setAuthentication(Request $request, $bundle = 'FOS')
$accesTokenString = $this->getBearerToken($request, $bundle);
}
catch (\Exception $e) {
$exception = $this->errorHandling($e);
return new JsonResponse(array(
'error' => $exception['error'],
'error_type' => $exception['type'],
'error_description' => $exception['error_description']), $exception['httpCode']);
return $this->jsonExceptionResponse($e);
}
if (isset($accesTokenString)) {
if ($bundle == 'FOS') {
Expand All @@ -81,11 +77,7 @@ public function setAuthentication(Request $request, $bundle = 'FOS')
try {
$oauthToken = $this->container->get('security.authentication.manager')->authenticate($oauthToken);
} catch (\Exception $e) {
$exception = $this->errorHandling($e);
return new JsonResponse(array(
'error' => $exception['error'],
'error_type' => $exception['type'],
'error_description' => $exception['error_description']), $exception['httpCode']);
return $this->jsonExceptionResponse($e);
}
}
else {
Expand Down Expand Up @@ -406,11 +398,7 @@ public function checkAccessGranted(Request $request, $bundle)
}
}
} catch (OAuth2AuthenticateException $e) {
$exception = $this->errorHandling($e);
return new JsonResponse(array(
'error' => $exception['error'],
'error_type' => $exception['type'],
'error_description' => $exception['error_description']), $exception['httpCode']);
return $this->jsonExceptionResponse($e);
}
}
elseif ($bundle == 'FOS') {
Expand Down Expand Up @@ -488,12 +476,12 @@ public function verifyAccessToken($tokenParam, $bundle = '')
}

/**
* Gets the right error message and code
*
* Generate a JSON response containing an error message, error type and code.
*
* @param unknown $e
* @return array $result
* @return JsonResponse
*/
private function errorHandling($e)
private function jsonExceptionResponse($e)
{
$result = array('type' => 'ERROR');
if ($e instanceof OAuth2AuthenticateException || $e->getPrevious() instanceof OAuth2AuthenticateException) {
Expand All @@ -518,7 +506,16 @@ private function errorHandling($e)
$result['error'] = $e->getCode();
$result['error_description'] = $this->container->get('translator')->trans($e->getMessage());
$result['httpCode'] = 500;
}
return $result;
$logger = $this->container->get('logger');
$logger->critical($e->getMessage() . $e->getTraceAsString());
}
return new JsonResponse(
array(
'error' => $result['error'],
'error_type' => $result['type'],
'error_description' => $result['error_description']
),
$result['httpCode']
);
}
}

0 comments on commit f96ae98

Please sign in to comment.