5

I want to throw a custom exception if the variable is null and return it as JSON. I've tried like this:

Controller

try {
    $check_api_key = $attendance_libraries->check_api_key($this->request);
    if ($check_api_key == null) {
        throw new NullException(false, 'API Key Not Found', null, 500);
    }
} catch (NullException $e) {
    return $e;
} catch (\Exception $e) {
    // do something else           
}

Custom Exception

<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Http\Response;

class NullException extends Exception
{
    public $is_success;
    public $message;
    public $code;
    public $data;

    public function __construct($is_success, $message, $code, $data = null, Exception $previous = NULL)
    {
        $this->is_success = $is_success;
        $this->message = $message;
        $this->code = $code;
        $this->data = $data;
    }

    public function render()
    {
        return response()->json([
            'success' => $this->is_success,
            'message' => $this->message,
            'data' => $this->data,
        ], $this->code);
    }
}

But when I am trying to test it using postman, the return I got is not the same as I wrote in NullException. It becomes like this: Postman response

1
  • 2
    Just replace return $e; with throw $e; in your controller. And really your method check_api_key() should be throwing the exception for you. Commented Feb 1, 2022 at 17:37

2 Answers 2

2

In your case you are returning the exception as a response instead of throwing it. That's why it's displayed like this.

You could just have to throw the exception without the try/catch:

$check_api_key = $attendance_libraries->check_api_key($this->request);

if ($check_api_key == null) {
    throw new NullException(false, 'API Key Not Found', null, 500);
}

The laravel error handler will catch the exception & render it.

EDIT: Or as @miken32 pointed out you could re throw the exception to handle other exceptions:

try {
 //...
} catch (NullException $e) {
  throw $e;
} catch (// other exceptions) {
}

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

You're right they should be doing throw $e instead of return $e but the try/catch construct is still needed because they want to catch other types of exceptions and handle them.
Yes you are right ! I was just simplifying the explanation.
Don't simplify to the point of removing functionality.
1

Instead of Returning the render method from Exception you are returning the Exception. And also you are passing the arguments in wrong order.

Route::get('exception', function () {
    try {
        $check_api_key = null;

        if ($check_api_key == null) {
            throw new NullException(
                is_success: false,
                message: "API Key Not Found",
                code: 500
            );
        }
    } catch (NullException $e) {
        return $e->render();
    } catch (\Exception $e) {
    }
});

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.