Giter Club home page Giter Club logo

Comments (15)

flugg avatar flugg commented on August 14, 2024

Hey Evgenii :)

Have you copy pasted all the lines from the render method of the package's exception handler? Including:

        if ($exception instanceof ApiException) {
            return $this->renderApiError($exception);
        }

from laravel-responder.

nasyrov avatar nasyrov commented on August 14, 2024

Hi @Flugger,

Thanks for the reply!
Yes I did, and this is my full method code:

    public function render($request, Exception $exception)
    {
        // Uncomment to debug travis-ci output
        // throw $exception;

        $this->transformException($exception);

        if ($exception instanceof ApiException) {
            return $this->renderApiError($exception);
        }

        return parent::render($request, $exception);
    }

from laravel-responder.

flugg avatar flugg commented on August 14, 2024

Hmm, that's weird. I assume you've imported the full Flugg\Responder\Exceptions\Http\ApiException file in the top?

What errors are you getting if you enable debug mode?

from laravel-responder.

nasyrov avatar nasyrov commented on August 14, 2024

@Flugger, my intent is to get rid of the Woops page and make sure all the responses either success or error return in JSON format.

For example, I don't have a default route, here is the message – http://realty-staging-api.herokuapp.com/

Another example, with an error – http://realty-staging-api.herokuapp.com/offers?filters[type]=50

from laravel-responder.

flugg avatar flugg commented on August 14, 2024

Yes, correct. Thats what the code above should fix. Perhaps Lumen throws a different exception on page not found than Laravel. I'll do some investigation and get back to you!

from laravel-responder.

nasyrov avatar nasyrov commented on August 14, 2024

In the second example, my application throws a standard UnexpectedValueException. Maybe I should re-catch in the handler?

from laravel-responder.

flugg avatar flugg commented on August 14, 2024

If your UnexpectedValueException extends ApiException it should automatically be caught with the code in your handler. I just created a Lumen application and did some testing and realised, there is no default exception transformer for page not found errors. I definitely think it should be included out of the box and will look into getting it into the v2 release. As a workaround, you can catch the 404 errors manually in the render method:

use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

if ($exception instanceof NotFoundHttpException) {
      return responder()->error('page_not_found', 404, 'The page you are looking for could not be found.');
}

from laravel-responder.

flugg avatar flugg commented on August 14, 2024

Did you find a solution?

I also just realised UnexpectedValueException is a standard PHP exception, so that would also need to be caught and handled, like the NotFoundHttpException above. Generally speaking, it might be a good idea to make exceptions that extend the abstract class ApiException and throw them instead. That way you will get the automatic converting to error responses without having to intercept them yourself.

from laravel-responder.

nasyrov avatar nasyrov commented on August 14, 2024

Hi @Flugger,

or maybe make a mapping like so:

$mapping = [
    UnexpectedValueException::class => NotFoundHttpException::class
];

from laravel-responder.

flugg avatar flugg commented on August 14, 2024

Yeah, that's definitely a good idea and something I've considered in the past, but stuff like the ValidationFailedException expects a validator argument which is hard to pull off with the array version.

from laravel-responder.

flugg avatar flugg commented on August 14, 2024

Hmm, what about allowing closures, so you can do this:

$mappings = [
    UnexpectedValueException::class => NotFoundHttpException::class,
    ValidationException::class => function ($exception) {
        return new ValidationFailedException($exception->validator);
    },
];

Edit: Just realised this would have to be done in a method as you can't put expressions into default properties.

from laravel-responder.

flugg avatar flugg commented on August 14, 2024

I've been playing around with it some more and so far this will be possible in v2:

    public function render($request, Exception $exception)
    {
        $this->convert($exception, [
            UnexpectedValueException::class, PageNotFoundException::class,
            ValidationException::class, function($exception) {
                throw new ValidationFailedException($exception->validator);
            }
        ]);

        $this->convertLaravelException($exception);

        if ($exception instanceof ApiException) {
            return $this->renderErrorResponse($exception);
        }

        return parent::render($request, $exception);
    }

Edit: updated the code

from laravel-responder.

nasyrov avatar nasyrov commented on August 14, 2024

Hi @Flugger,

That looks good to me, I thought about my problem as well, probably the best thing to do in my case is to wrap the functionality with try ... catch.

try {
    if (some critical stuff)
        throw new UnexpectedValueException;
} catch(UnexpectedValueException $e) {
    return responder()->error('oops_1');
} catch(ValidationException $e) {
    return responder()->error('oops_2');
}

The thing is, some of these exceptions could be thrown by 3rd party extensions that I installed via Composer, so I cannot update them and extend from ApiException.

from laravel-responder.

flugg avatar flugg commented on August 14, 2024

Yeah, thats usually how I solve it in my apps :) You can of course also catch the exceptions and rethrow your own versions that extend ApiException.

from laravel-responder.

nasyrov avatar nasyrov commented on August 14, 2024

Alright, thanks a lot! I'm closing this for now ...

from laravel-responder.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.