2019-08-21 20:17:44 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Exceptions;
|
|
|
|
|
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException as SymfonyHttpException;
|
2019-11-19 23:06:07 +08:00
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
|
2019-08-21 20:17:44 +08:00
|
|
|
|
2019-11-19 23:06:07 +08:00
|
|
|
/**
|
|
|
|
* Abstract class for an exception which needs to satisty the RFC 78707 interface
|
|
|
|
*/
|
|
|
|
abstract class AbstractHttpException extends SymfonyHttpException implements HttpExceptionInterface
|
2019-08-21 20:17:44 +08:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Return the RFC 7807 error type (without the URL root)
|
|
|
|
*/
|
|
|
|
abstract public function getErrorType(): string;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the detailed error string
|
|
|
|
*/
|
|
|
|
abstract public function getErrorDetails(): string;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return an array with the error details, merged with the RFC7807 response
|
|
|
|
*/
|
|
|
|
abstract public function getErrorMetadata(): array;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the error message as JSON
|
|
|
|
*/
|
|
|
|
public function getJson()
|
|
|
|
{
|
|
|
|
$response = [];
|
|
|
|
|
|
|
|
$response['type'] = config('phpvms.error_root').'/'.$this->getErrorType();
|
|
|
|
$response['title'] = $this->getMessage();
|
|
|
|
$response['details'] = $this->getErrorDetails();
|
2019-08-21 20:19:49 +08:00
|
|
|
$response['status'] = $this->getStatusCode();
|
2019-08-21 20:17:44 +08:00
|
|
|
|
|
|
|
// For backwards compatibility
|
|
|
|
$response['error'] = [
|
|
|
|
'status' => $this->getStatusCode(),
|
|
|
|
'message' => $this->getErrorDetails(),
|
|
|
|
];
|
|
|
|
|
|
|
|
return array_merge($response, $this->getErrorMetadata());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a response object that can be used by Laravel
|
|
|
|
*
|
2019-11-19 23:06:07 +08:00
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response
|
2019-08-21 20:17:44 +08:00
|
|
|
*/
|
|
|
|
public function getResponse()
|
|
|
|
{
|
2019-08-23 00:26:46 +08:00
|
|
|
$headers = [];
|
|
|
|
$headers['content-type'] = 'application/problem+json';
|
|
|
|
$headers = array_merge($headers, $this->getHeaders());
|
|
|
|
|
2019-11-19 23:06:07 +08:00
|
|
|
return response()->json($this->getJson(), $this->getStatusCode(), $headers);
|
2019-08-21 20:17:44 +08:00
|
|
|
}
|
|
|
|
}
|