You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In one of my endpoints, I validate a date param with the following code:
public function index(Request $request)
{
if ($date = $request->query->get('date')) {
try {
$date = new \DateTime($date);
} catch (\Exception $e) {
throw new Exception\BadRequest();
}
}
// ...
}
If I make a request to /endpoint?date=foobar I get the following response:
{"code":0,"message":"Bad Request","details":[]}{"code":0,"message":"DateTime::__construct(): Failed to parse time string (foobar) at position 0 (f): The timezone could not be found in the database","details":[]}
There's the BadRequest exception I thrown followed by the one I'm supposed to have caught.
The api consumers don't need to know that I suck at parsing dates, and that's why I catch the exception and throw a generic one. When debug is false, these kind of error messages should not be returned to the users.
{"code":0,"message":"Bad Request","details":[]}{"code":0,"message":"DateTime::__construct(): Failed to parse time string (foobar) at position 0 (f): The timezone could not be found in the database","details":[]}
Have you got exactly this response?
And you're right when debug is false you should see that message. So there is a bug, I'll check it.
I do not use the sample project, but it's pretty close. I use fractal instead of the serializer and generate api docs with swagger-php ... but that's about it.
I can test with the sample project to see if the same issue exist.
It's the logger in the sample project that overwrites the DateTime error with 'Server Error'. If I disable the logger, I've got the exact same Response than in my project.
Activity
adammbalogh commentedon Jan 7, 2015
What would you like to see as a response?
The response you see is generated by the default exception handler. You throw a
BadRequestexception and phprest generates a default wrapped response.marcaube commentedon Jan 7, 2015
I only expected:
{"code":0,"message":"Bad Request","details":[]}The api consumers don't need to know that I suck at parsing dates, and that's why I catch the exception and throw a generic one. When debug is
false, these kind of error messages should not be returned to the users.adammbalogh commentedon Jan 7, 2015
I've just seen you get an invalid json response.
{"code":0,"message":"Bad Request","details":[]}{"code":0,"message":"DateTime::__construct(): Failed to parse time string (foobar) at position 0 (f): The timezone could not be found in the database","details":[]}Have you got exactly this response?
And you're right when debug is
falseyou should see that message. So there is a bug, I'll check it.Do you use the sample project?
marcaube commentedon Jan 7, 2015
Yeah I got exactly this response.
I do not use the sample project, but it's pretty close. I use fractal instead of the serializer and generate api docs with swagger-php ... but that's about it.
I can test with the sample project to see if the same issue exist.
adammbalogh commentedon Jan 7, 2015
I have to update the
phprest\phprestdependency in the sample project.I am going to try to reproduce the error.
marcaube commentedon Jan 7, 2015
I just installed the sample project and tweaked the
Camera->get()method to reproduce my problemThe response is still invalid JSON, but no mention of
DateTime{"code":0,"message":"Server Error","details":[]}{"code":0,"message":"Server Error","details":[]}marcaube commentedon Jan 7, 2015
It's the logger in the sample project that overwrites the
DateTimeerror with'Server Error'. If I disable the logger, I've got the exact same Response than in my project.adammbalogh commentedon Jan 7, 2015
Could you test it please? It works for me now.
marcaube commentedon Jan 7, 2015
I updated and it works. What's up with the
$config->setApiVersionHandler()? It was removed?adammbalogh commentedon Jan 7, 2015
Yes it was. I've to find a better place for it.
Merge pull request phprest#4 from ayanozturk/improve-scrutinizer