-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Improve exception message for DioException.badResponse #1998
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,8 +86,7 @@ class DioException implements Exception { | |
}) => | ||
DioException( | ||
type: DioExceptionType.badResponse, | ||
message: 'The request returned an ' | ||
'invalid status code of $statusCode.', | ||
message: _badResponseExceptionMessage(statusCode), | ||
requestOptions: requestOptions, | ||
response: response, | ||
error: null, | ||
|
@@ -211,4 +210,38 @@ class DioException implements Exception { | |
} | ||
return msg; | ||
} | ||
|
||
/// Because of [ValidateStatus] we need to consider all status codes when | ||
/// creating a [DioException.badResponse]. | ||
static String _badResponseExceptionMessage(int statusCode) { | ||
var message = ''; | ||
if (statusCode >= 100 && statusCode < 200) { | ||
message = | ||
'This is an informational response - the request was received, continuing processing'; | ||
} else if (statusCode >= 200 && statusCode < 300) { | ||
message = | ||
'The request was successfully received, understood, and accepted'; | ||
} else if (statusCode >= 300 && statusCode < 400) { | ||
message = | ||
'Redirection: further action needs to be taken in order to complete the request'; | ||
} else if (statusCode >= 400 && statusCode < 500) { | ||
message = | ||
'Client error - the request contains bad syntax or cannot be fulfilled'; | ||
} else if (statusCode >= 500 && statusCode < 600) { | ||
message = | ||
'Server error - the server failed to fulfil an apparently valid request'; | ||
} else { | ||
message = | ||
'A response with a status code that is not within the range of inclusive 100 to exclusive 600' | ||
'is a non-standard response, possibly due to the server\'s software.'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems to be a duplicated dot. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, but I rather want the message to be helpful than concise. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Hmm, what I mean is the last dot of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah I see, yeah, that might be redundant, but this message is aimed at people who are not yet that knowledgeable of the HTTP spec and so on. So I think it's a rather helpful addition, even if it might seem superfluous for us knowledgeable people. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. DioException [bad response]: This exception was thrown because the response has a status code of 0 and RequestOptions.validateStatus was configured to throw for this status code.
The status code of 0 has the following meaning: "A response with a status code that is not within the range of inclusive 100 to exclusive 600is a non-standard response, possibly due to the server's software."
.Read more about status codes at https://developer.mozilla.org/en-US/docs/Web/HTTP/Status Here it should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. which made that dot redundant |
||
} | ||
|
||
return '' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not really aware of the benefits of a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
You just don't have to constantly write |
||
'This exception was thrown because the response has a status code of $statusCode ' | ||
'and RequestOptions.validateStatus was configured to throw for this status code.\n' | ||
'The status code of $statusCode has the following meaning: "$message"\n.' | ||
'Read more about status codes at https://developer.mozilla.org/en-US/docs/Web/HTTP/Status\n' | ||
'In order to resolve this exception you typically have either to verify ' | ||
'and fix your request code or you have to fix the server code.'; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used to write
final String message
if it's declared in every single flow.