Giter VIP home page Giter VIP logo

Comments (4)

veewee avatar veewee commented on May 28, 2024 1

Sorry for the delay @chekalsky and thanks for picking up the PR.

I feel the same way about the PR and am going to decline it in a moment.
After some thinking, it turns out that it is already possible to achieve what you want with the tools we currently provide.
Let me share you my vision:

You can create a new transformer and specify a good priority for it.
This transformer takes a list of exception classes it either makes visible or otherwise obfuscates.
Something along these lines:

<?php

use Phpro\ApiProblem\ApiProblemInterface;
use Phpro\ApiProblem\Http\ExceptionApiProblem;
use Phpro\ApiProblemBundle\Transformer\ExceptionTransformerInterface;

class PublicExceptionTransformer implements ExceptionTransformerInterface
{
    public function __constuct(
        private string ... $publicExceptionClasses
    ) {
    }

    public function transform(\Throwable $exception): ApiProblemInterface
    {
        $exceptionClass = get_class($exception);
        if (in_array($exceptionClass, $this->publicExceptionClasses, true)) {
            return new ExceptionApiProblem($exception);
        }

        // We could add a constructor variable to overwrite the message or add a withDetails() immutable method.
        return new ExceptionApiProblem($exception, $exceptionClass);
    }

    public function accepts(\Throwable $exception): bool
    {
        return true;
    }
}

(You could make the code even smarter and specify a PublicExceptionInterface inside your own codebase. That interface could then be used to determine wether the exception is public or not.)

We could accept a PR for a default implementation of this listener.

What do you think?

from api-problem-bundle.

veewee avatar veewee commented on May 28, 2024

Yeah, that would make sense ....

The best option IMO is to put this logic inside the exception apiproblem:
https://github.com/phpro/api-problem/blob/master/src/Http/ExceptionApiProblem.php

An other option is to add an exception transformation that does this job.

Care to create a PR?

from api-problem-bundle.

chekalsky avatar chekalsky commented on May 28, 2024

@veewee Prepared PR but I don't feel good about it, looks like a big change in core functionality people can rely on. Maybe better create default Transformer for Symfony where we will not reveal exception message?

from api-problem-bundle.

mesilov avatar mesilov commented on May 28, 2024

i think this feature must depend with env - dev (more detailed) prod (hide trace and other sensitive information)

from api-problem-bundle.

Related Issues (8)

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.