-
Notifications
You must be signed in to change notification settings - Fork 1
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 error handling #3
base: main
Are you sure you want to change the base?
Conversation
54a7f73
to
bd7ba1b
Compare
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.
Happy to provide feedback if you're looking for it otherwise disregard :D
This is loads better than what exists so LGTM.
@@ -102,3 +92,47 @@ export async function request( | |||
|
|||
return json.values; | |||
} | |||
|
|||
function handleError(errorResponse: string | object) { |
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.
You could add a return type of never
to this function.
https://www.typescriptlang.org/docs/handbook/2/functions.html#never
return error; | ||
} | ||
|
||
try { | ||
const object = JSON.parse(error); | ||
|
||
if (object && typeof object === "object") { | ||
return object; |
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.
In both these return cases, there's an assumption that the objects contain an error_message
. One way you could solve this is to move the || "CiviCRM request failed",
from line 126 to line 135.
Requests will now throw a
CiviCRMRequestError
with the message provided by the API, if any, and adetail
property containing any other details provided by the API.