-
Notifications
You must be signed in to change notification settings - Fork 17
Allow to configure a default type map #50
Description
- I was not able to find an open or closed issue matching what I'm seeing.
- This is not a question. (Questions should be asked on slack (Signup for Slack here) or our forums.)
Currently, when the ProblemDetailsResponseFactory::createResponse method is called without a type, it tries to infer it from the status, generating values like https://httpstatus.es/404 or https://httpstatus.es/500.
This mainly happens when the ProblemDetailsNotFoundHandler is hit, or the ProblemDetailsMiddleware catches an exception not implementing ProblemDetailsExceptionInterface.
If your API has a specific pattern when generating the types for other errors, it's a bit inconsistent that these two errors follow a different pattern.
It would be nice to be able to define a config map where you set the default type to be used for "every" status code when a value for type was not provided. Something like this:
<?php
return [
'zend_problem_details' => [
'default_type_fallbacks' => [
404 => '/my-app/errors/resource-not-found',
500 => '/my-app/errors/unknown',
],
],
];Then, by injecting this map in the ProblemDetailsResponseFactory, the createTypeFromStatus method could be refactored to look like this:
private function createTypeFromStatus(int $status) : string
{
return $this->defaultTypeFallbacks[$status] ?? sprintf('https://httpstatus.es/%s', $status);
}So it would continue working the same when the config map is not defined, but every user would have control over the values generated for the type.
I'm open to implement this if you think it's useful.