Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 44 additions & 10 deletions src/lib/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,6 @@ function authenticationHeader(auth?: Authentication): string {
throw new Error("auth must contain apiKey, jwt, or username/password");
}

function handleError(error: string) {
if (this.debug) {
console.error(error);
console.groupEnd();
}

// TODO: make this error more informative
throw new Error("CiviCRM request failed");
}

export function bindRequest(
requestFn: BaseRequestFn<any, any>,
config: ClientConfig<any, any>,
Expand Down Expand Up @@ -102,3 +92,47 @@ export async function request(

return json.values;
}

function handleError(errorResponse: string | object) {

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

const error = new CiviCRMRequestError(errorResponse);

if (this.debug) {
console.error(error);
console.groupEnd();
}

throw error;
}

class CiviCRMRequestError extends Error {
name = "CiviCRMRequestError";
message: string;
detail: any;

parseError(error: string | { error_message: string }) {
if (typeof error === "object") {
return error;
}

try {
const object = JSON.parse(error);

if (object && typeof object === "object") {
return object;
Comment on lines +114 to +121

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.

}
} catch (e) {}

return {
error_message: error || "CiviCRM request failed",
};
}

constructor(error: any) {
super();

const { error_message, ...detail } = this.parseError(error);

this.message = error_message;
this.detail = detail;
}
}
7 changes: 6 additions & 1 deletion test/api3.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,12 @@ describe("debug", () => {
} catch {}

expect(consoleSpy.group).toHaveBeenCalled();
expect(consoleSpy.error).toHaveBeenCalledWith("Internal Server Error");
expect(consoleSpy.error).toHaveBeenCalledWith(
expect.objectContaining({
name: "CiviCRMRequestError",
message: "Internal Server Error",
}),
);
expect(consoleSpy.groupEnd).toHaveBeenCalled();
});
});
7 changes: 6 additions & 1 deletion test/api4.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,12 @@ describe("debug", () => {
} catch {}

expect(consoleSpy.group).toHaveBeenCalled();
expect(consoleSpy.error).toHaveBeenCalledWith("Internal Server Error");
expect(consoleSpy.error).toHaveBeenCalledWith(
expect.objectContaining({
name: "CiviCRMRequestError",
message: "Internal Server Error",
}),
);
expect(consoleSpy.groupEnd).toHaveBeenCalled();
});
});
Loading