Laravel exception handling - how to handle exceptions with API and Html

Api call

http://localhost:8888/api/v1/users/100 //doesn't exist

Html call

http://localhost:8888/admin/users/100 //doesn't exist

Obviously, I do not want the Html Call exception to return json data, and I do not want Api Call to return the Html Data.

I am not an exception handler in the controller. I handle exception handling in my UserRepository. This way my controllers just return the result from the user repository.

class Sentry2UserRepository implements UserInterface {
public function findById($id) {
    try {
        return Sentry::findUserById($id);
    }
    catch (\Cartalyst\Sentry\Users\UserNotFoundException $e) {
    // Do something here
    return false;
        }
}
}

Question 1: What is the normal / correct way to pass the error back to the controller so that it knows what to display?

Question 2: Is there a standard json API format for exceptions / errors?

Question 3: Is it right to use the web interface for internal JsonApi? Or am I doing everything right at the moment using my WebUi controllers. Request the same repositories as Api?

+4
2

filters.php:

App::error(function(Exception $exception, $httpCode)
{
    if (Request::is('api/*')){
         return Response::json( ['code' => $exception->getCode(), 'error' => $exception->getMessage()], $httpCode );
    }else{
         $layout = View::make('layouts.main');
         $layout->content = View::make('errors.error')->with('code', $exception->getCode())->with('error', $exception->getMessage())->with('httpCode',$httpCode);
         return Response::make($layout, $httpCode);
    }
});
+3

, , Sentry2UserRepository , , IMO.

1: / , , ?

, , , . so that it will know what to display, , , . , , return $e->getMessage(), , . , , catch:

try{
    // ...
}
catch( Exception $e )
{
    if ($e instanceof UserNotFoundException) {
        // it an instance of UserNotFoundException, return accordingly
    }
    elseif ($e instanceof SomethinElseException) {
        // it an instance of SomethinElseException, return accordingly
    }
}

, catch, ..

class AnException extends Exception 
{
    public function customErrorMessage() 
    {
        return `AnException occurred!`
    }
}

class AnotherException extends Exception 
{
    public function customErrorMessage() 
    {
        return `AnotherException occurred!`
    }
}

catch, ..

try 
{
    // ...
}

catch(AnException $e) 
{
    return $e->customErrorMessage();
}

catch(AnotherException $e) 
{
    return $e->customErrorMessage();
}
catch(Exception $e)
{
    return $e->getMessage();
}

2: json API /?

3: - JsonApi? WebUi. , Api?

, api, , . ,

class Sentry2UserRepository implements UserInterface {
    public function findById($id) {
        try {
            return Sentry::findUserById($id);
        }
        catch (\Cartalyst\Sentry\Users\UserNotFoundException $e) {
            // Do something here
            return false;
        }
    }
}

, , -

if(findById(5)) {
    // found and dump it to the view
}
else {
    // show "Not Found !", false will be back only for UserNotFoundException 
}

UserNotFoundException catch

return $e; // or anything else (maybe an array containing status and message)

if(findById(5)) {
    // found and dump it to the view
}

, $e oe , , somrthing,

$result = findById(5);
if($result && $result->code && $result->code === 0) {
    // something according to code
}

, , -

if($result && $result->code) {
    // error happened, now determine the code
    switch($result->code){
        case 0:
        // show message
        break;

        case 1:
        // show message
        break;
    }
}

, IMO, , , , , . ? KISS. , .

0

All Articles