4

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 don't want the Html Call exception to return json data and I don't want the Api Call to return Html Data.

I am not exception handling in the controller. I am exception handling in my UserRepository. As such, my controllers are just returning a 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 / proper way of passing an error back to controller so that it will know what to display?

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

Question 3: Is it good practice for a Web UI to consume an internal JsonApi? Or am I doing things the right way at the moment with my WebUi controllers Querying the same Repositories as the Api?

2 Answers 2

4

Try this magic in your 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);
    }
});
Sign up to request clarification or add additional context in comments.

Comments

0

First of all, I think your approach in Sentry2UserRepository is not bad, it's ok, IMO.

Question 1: What is the normal / proper way of passing an error back to controller so that it will know what to display?

Well, IMO, depending on the application you should determine how you should handle exceptions. You mentioned so that it will know what to display and in this case it depends on how and what information you need from an exception to take the next action after an exception occured. now if you need the error message then you may return the return $e->getMessage() so you'll exactly know what actually happened. There are many ways to do this, for example, using a single catch :

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

Also, you can use different custom exception classes and may use multiple catch blocks, i.e.

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

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

Then catch using multiple catch blocks, i.e.

try 
{
    // ...
}

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

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

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

Question 3: Is it good practice for a Web UI to consume an internal JsonApi? Or am I doing things the right way at the moment with my WebUi controllers Querying the same Repositories as the Api?

Actually I don't know about such an api and you are doing right, IMO. It's because, you have this

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

So, it's possible to write code in controller something like this

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

But, if you had this in your UserNotFoundException catch

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

Then It's not possible to write this simple code

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

Because, is statement will be true for $e object oe for an array, so you have to check it again, using somrthing like this

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

Or maybe, something like this

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

        case 1:
        // show message
        break;
    }
}

So, IMO, why, you need to show the user, what error happened in your application, why not just to states, either you got data or you didn't get it. isn't it simple ? Just KISS. This is my opinion only, that's it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.