jalendport / craft-fetch

Utilise the Guzzle HTTP client from within your Craft templates.
MIT License
24 stars 13 forks source link

Add more detailed error responses from original request #8

Open noahjahn opened 3 years ago

noahjahn commented 3 years ago

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.