Conversation
Avoid KeyError when optional fields are missing in Onfleet error response
PR SummaryThis pull request improves error handling in the Changes
Impact
This is a small but important change that enhances the reliability of the library when dealing with unexpected API responses. |
|
While this is a small change to error handling, it's addressing an important bug where the wrong type of error was being raised. Adding tests would help verify that the fix works as expected and prevent regression in the future. Consider adding tests that:
|
There was a problem hiding this comment.
Caution
Changes requested ❌
Reviewed everything up to ffbb6e1 in 2 minutes and 12 seconds. Click for details.
- Reviewed
15lines of code in1files - Skipped
0files when reviewing. - Skipped posting
1draft comments. View those below. - Modify your settings and rules to customize what types of comments Ellipsis leaves. And don't forget to react with 👍 or 👎 to teach Ellipsis.
1. onfleet/request.py:68
- Draft comment:
Using .get() avoids KeyError for optional fields, but error_code is later used in numeric comparisons (e.g., line 75). If the 'error' key is missing, error_code becomes None, which will raise a TypeError. Consider providing a default numeric value, e.g. error['message'].get('error', 0), or adding a check before comparisons. - Reason this comment was not posted:
Marked as duplicate.
Workflow ID: wflow_R2AgANdeGVOuPNhw
You can customize by changing your verbosity settings, reacting with 👍 or 👎, replying to comments, or adding code review rules.
Providing a default numeric value (e.g. .get('error', 0)) to ensure safe comparisons.
Co-authored-by: ellipsis-dev[bot] <65095814+ellipsis-dev[bot]@users.noreply.github.com>
Describe the solution
Switches from direct key access to
.get()for optional fields (error,request,cause) in the Onfleet API error payload. This prevents aKeyErrorwhen those fields are missing and ensures the intended error (e.g.,HttpError,ValidationError, etc.) is raised instead.Fixed
Fixes #30 – prevents crashes caused by missing fields in Onfleet error responses. Now the correct exception is raised with partial error data instead of blowing up with a
KeyError.Important
Switches to
.get()for optional fields inonfleet/request.pyto preventKeyErrorand ensure correct exceptions are raised.onfleet/request.py, switch from direct key access to.get()forerror,request, andcausefields in error payloads.KeyErrorwhen fields are missing, ensuring correct exceptions are raised.This description was created by
for ffbb6e1. You can customize this summary. It will automatically update as commits are pushed.