Skip to content

Instantly share code, notes, and snippets.

@mahammad
Last active July 31, 2024 08:21
Show Gist options
  • Select an option

  • Save mahammad/e8a03cb6a97fac831b63da75d27922a9 to your computer and use it in GitHub Desktop.

Select an option

Save mahammad/e8a03cb6a97fac831b63da75d27922a9 to your computer and use it in GitHub Desktop.
 public function render($request, Throwable $exception)
    {
        // is this request asks for json?
        if($request->is('api/*')){
            if ( !empty($exception) ) {
                $response = [
                    'error' => __('messages.request.response.execute')
                ];
                // If debug mode is enabled
                if (config('app.debug')) {
                    //$response['exception'] = get_class($exception); // Reflection might be better here
                    $response['message'] = $exception->getMessage();
                    $response['file'] = $exception->getFile();
                    $response['line'] = $exception->getLine();
                    //$response['trace'] = $exception->getTrace();
                }
                $status = 400;
                if($exception instanceof ValidationException){
                    return $this->convertValidationExceptionToResponse($exception, $request);
                }else if($exception instanceof AuthenticationException){
                    $status = 401;
                    $response['error'] = __('messages.request.response.authentication');
                }else if($exception instanceof \PDOException){
                    $status = 500;
                    $response['error'] = __('messages.request.response.query');
                }else if($this->isHttpException($exception)){
                    $status = $exception->getStatusCode();
                    $response['error'] = __('messages.request.response.error');
                }else{
                    $status = method_exists($exception, 'getStatusCode') ? $exception->getStatusCode() : 400;
                }
                $response['status'] = $status;
                return response()->json($response,$status);
            }
        }
        return parent::render($request, $exception);
    }

lang file message.php

<?php
return [
    'request'=>[
        'response'=>[
            'error' => 'İstek Hatası',// Request error!
            'execute' => 'Üzgünüz, isteğinizi yerine getiremiyorum.',// Sorry, can not execute your request.
            'query' => 'Sorgu isteğiniz tamamlanamıyor!',// Can not finish your query request!
            'authentication' => 'Kimlik doğrulaması tamamlanamıyor!',// Can not finish authentication!
        ]
    ]
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment