Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Incorrect http status code if value argument resolvers can't resolve value (invalid value) #52782

Closed
@ZhukV

Description

@ZhukV

Description

If we use attributes for map query for request payload, and value is invalid (use filter FILTER_VALIDATE_INT and pass string, pagination as an example, where we pass page and limit parameters), we receive NotFoundHttpException, but it's a very difficult, because about Http status codes Not Found (404) indicate was resource was not found in system and you pass invalid path, but, in our case system correct resolve resource target but can't process because input parameters are invalid. By default, more systems returns 400 status code (BadRequest).

And, in next problem is unable to correct catch this error in any components (kernel.exception event) for render correct response.

As an example. We have API route for load products:

#[Route(path: '/api/products')]
public function __invoke(
  Request $request,
  #[MapQueryParameter(filter: FILTER_VALIDATE_INT)] int $page = 1,
  #[MapQueryParameter(filter: FILTER_VALIDATE_INT)] int $limit = 50
): Response {
 // code here
}

And, if I try to call GET /api/products?page=foo&limit=bar:

  1. System returns NotFoundHttpException instead of BadRequest (via http status codes).
  2. I can't add listener and catch this error for correct returns response, because catch NotFoundHttpException in this case will be hard.

I this what better will be create new type exception for invalid parameters in ValueResolverInterface, extended from BadRequestHttpException and possible to receive violations for correct get error info on kernel.exception listeners.

As an example:

class CannotResolveArgumentValueException extends BadRequestHttpException
{
    private ConstraintViolationListInterface $violations;
    
    public function __construct(string $message = '', \Throwable $previous = null, int $code = 0, array $headers = [], ConstraintViolationListInterface $violations = null)
    {
        parent::__construct($message, $previous, $code, $headers);
        $this->violations = $violations;
    }

    public function getViolations(): ConstraintViolationListInterface
    {
        return $this->violations;
    }
}

If developer not catch this error in any listeners, system returns 400 status code (indicate what some parameters are invalid). But, developer can add listener and catch CannotResolveArgumentValueException error and get violations if required.

Note: it's affected all value resolvers.

Example

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions