Adamski
Adamski

Reputation: 3685

Laravel testing and text-only exception messages

I am trying to get readable error messages using Codeception with Laravel. I have found how to override the App::error function to give me text or json based responses. However I would like to somehow set the errors so that it only displays html error pages when not calling from the tests.

I realise the functional tests I am running use the PhpBrowser class so may not be able to check ...

Any hints much appreciated.

Code below:

App::error(function(Exception $exception, $code, $fromConsole)
{

    Log::error($exception);

    if($fromConsole) {
        return Response::make($exception->getMessage());

    }

    if ( Request::header('accept') === 'application/json' )
    {
        return Response::json([
            'error' => true,
            'message' => $exception->getMessage(),
            'code' => $code],
            $code
        );
    } 
});

Upvotes: 1

Views: 374

Answers (1)

sjdaws
sjdaws

Reputation: 3536

You can use App::environment() to get the current environment.

App::error(function(Exception $exception, $code, $fromConsole)
{

    Log::error($exception);

    if($fromConsole) {
        return Response::make($exception->getMessage());
    }

    if ( App::environment() == 'production' )
    {
        return Response::json([
            'error' => true,
            'message' => $exception->getMessage(),
            'code' => $code],
            $code
        );
    } 
});

Upvotes: 1

Related Questions