Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

238
Views
Laravel create custom exception with parameters & return as JSON

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

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Instead of returning the render method of Exception , it is returning Exception . And you're also passing the arguments in the 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) { } });
over 4 years ago · Santiago Trujillo Report

0

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

You may have to throw the exception without trying/catching:

 $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); }

Laravel's error handler will catch the exception and 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) { }

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!