Skip to content

Instantly share code, notes, and snippets.

@atomjoy
Last active May 4, 2025 09:14
Show Gist options
  • Save atomjoy/e1832694aa43dff3e9e97e2ceaeaeaea to your computer and use it in GitHub Desktop.
Save atomjoy/e1832694aa43dff3e9e97e2ceaeaeaea to your computer and use it in GitHub Desktop.
Laravel 12 custom json exception class.

Custom Json Exception in Laravel

Laravel 12 custom json exception class.

JsonException Class

<?php

namespace App\Exceptions;

use Exception;

class JsonException extends Exception
{
    /**
     * Report the exception.
     *
     * @return bool|null
     */
    public function report()
    {
        // Enable default logging
        return false;
    }

    /**
     * Render the exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function render($request)
    {
        // Create json response
        return response()->json([
            'message' => $this->message ?? 'Unknown Exception',
        ], ($this->code >= 100 && $this->code < 600) ? $this->code : 422);
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment