craft-fetch icon indicating copy to clipboard operation
craft-fetch copied to clipboard

Add more detailed error responses from original request

Open noahjahn opened this issue 4 years ago • 0 comments

Use case:
For an API request that responds back with detailed error messages for how to correct an issue, or exactly what went wrong, it would be nice to have the original response body returned when an Exception is thrown.

I also chose to add the status code from the HTTP request as well. A good example of this would be to display different messages to the user in a template for why the request failed.

My specific use case for this is searching for a term in an API. If the term is not found the API responds with 404, since the resource could not be found. I wanted to be able to handle this on the front end:

{% if response.statusCode == 404 %}
    <p class="text-warning">Record not found name {{record}} not found</p>
{% elseif response.statusCode == 400 %}
    <p class="text-danger">Record type is not supported</p>
{% else %}
    <p class="text-danger">{{error|json_encode(constant('JSON_PRETTY_PRINT'))}}</p>
{% endif %}

Side notes:
I didn't see any documentation on contributing to this project. Hoping this is okay.

noahjahn avatar Sep 18 '20 02:09 noahjahn