-
-
Notifications
You must be signed in to change notification settings - Fork 363
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for error handling in Orval built in fetch #1839
Comments
@oferitz
|
I want to preserve the original error so something like: fetch('https://api.example.com/data')
.then(async response => {
if (!response.ok) {
const errorData = await response.json();
// Preserve the original status and error details
const error = new Error(errorData.message || response.statusText);
error.status = response.status;
error.data = errorData;
throw error;
}
return response.json();
})
.catch(error => {
// Handle both network errors and response errors
console.error('Status:', error.status);
console.error('Message:', error.message);
console.error('Data:', error.data);
}); |
@oferitz |
@soartec-lab Thanks for your feedback. I understand your point about implementation variations. Let me propose a more standardized approach that strictly follows the Response API specification.
This minimal implementation could serve as a foundation that users can extend based on their specific needs. Would you consider supporting this basic level of error handling in Orval? // Basic error handling aligned with Response API spec
fetch('https://api.example.com/data')
.then(async response => {
if (!response.ok) {
// Using only standard Response API properties
const error = new Error(response.statusText);
error.status = response.status;
throw error;
}
return response.json();
})
.catch(error => {
// Handle errors with standard Response properties
console.error(`${error.status}: ${error.message}`);
}); |
I also understand the argument that it improves convenience. |
That's a valid point—you want to allow the user to choose whether or not to throw an error. I totally get it. For this, I have one last suggestion for your consideration. If it's not accepted, I'll go ahead and close the issue 🙂. My suggestion is to add the ability to throw an error, as described in my basic level of support above, but to make it configurable via Orval. This would work similarly to how users can choose whether to include the HTTP response return type. override: {
fetch: {
includeHttpResponseReturnType: false,
// New suggested config
throwOnException: true
}
} Let me know what you think. |
I understand that this issue has been discussed here: #1387, and the suggested solution is to create a custom fetch.
However, creating a custom fetch can sometimes be an overhead and introduce unwanted side effects, as highlighted in this issue: #1838.
Given these challenges, I believe it would be reasonable to consider adding support for error handling directly in Orval's built-in fetch. This enhancement would simplify the developer experience and avoid the need for custom implementations in many cases.
Your consideration of this feature would be much appreciated!
The text was updated successfully, but these errors were encountered: