Api: Custom Exception Responses in laravel

Created on 9 May 2016  路  9Comments  路  Source: dingo/api

In the wiki you see the following to register exception responses, this seems to be specific for lumen. Where and how to I do this in a laravel 5.2 installation.

app('Dingo\Api\Exception\Handler')->register(function (Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException $exception) {
    return Response::make(['error' => 'Hey, what do you think you are doing!?'], 401);
});

Most helpful comment

@SteveAzz you should put your code in 'boot()' method

All 9 comments

Hey, so here is how i did it :)

  1. in app/Providers create a new ServiceProvider Ex: ExceptionsServiceProvider.php
  2. in your config/app.php register the new provider AppProvidersExceptionsServiceProvider::class,
  3. in the register method of the provider just call the app('DingoApiExceptionHandler')->register(function(){ ... });

Ex of provider:

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Routing\Router;

/**
 * Class ExceptionsServiceProvider - Hacky?!
 * @package App\Providers
 */
class ExceptionsServiceProvider extends ServiceProvider
{
    /**
     * @return void
     */
    public function boot(){}

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        app('Dingo\Api\Exception\Handler')->register(function (\Illuminate\Database\Eloquent\ModelNotFoundException $exception) {
            throw new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
        });
    }
}

Razvan.

That worked, thank you :)

You should close this issue if it's resolved - Jason already has a enough to go through!

Hi @SteveAzz,
i did it the same but i have error:
Unresolvable dependency resolving [Parameter #1 [ array $format ]] in class DingoApiExceptionHandler
Can you suggest solution?

@cona88 Can you please show your code

@SteveAzz
In my config/app.php
/* * Application Service Providers... */ App\Providers\AppServiceProvider::class, App\Providers\AuthServiceProvider::class, App\Providers\EventServiceProvider::class, App\Providers\RouteServiceProvider::class, App\Providers\ExceptionsServiceProvider::class,

in Providers/ExceptionsServiceProvider.php
`

namespace AppProviders;

use IlluminateSupportServiceProvider;

class ExceptionsServiceProvider extends ServiceProvider
{

public function boot(){}


public function register()
{
    app('Dingo\Api\Exception\Handler')->register(function (Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException $exception) {
        return Response::make(['error' => 'Hey, what do you think you are doing!?'], 401);
    });
}

}`
Please help!

Hello, I have the following in my register method

/**
 * Register the service provider.
 *
 * @return void
 */
public function register()
{
    app('Dingo\Api\Exception\Handler')->register(function (\Illuminate\Database\Eloquent\ModelNotFoundException $exception) {
        throw new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
    });
}

Hope this helps, if you fix it tell me how you fixed it please

@SteveAzz
I fixed it,
In the app.php: i move App\Providers\ExceptionsServiceProvider::class, to the end of 'providers'

@SteveAzz you should put your code in 'boot()' method

Was this page helpful?
0 / 5 - 0 ratings