Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/twenty-bananas-cheer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@exactly/server": patch
---

🥅 fingerprint api errors by response message
28 changes: 27 additions & 1 deletion server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,33 @@ frontend.use(
app.route("/", frontend);

app.onError((error, c) => {
captureException(error, { level: "error", tags: { unhandled: true } });
let fingerprint: string[] | undefined;
if (error instanceof Error) {
const status = error.message.slice(0, 3);
const hasStatus = /^\d{3}$/.test(status);
const hasBodyFormat = error.message.length === 3 || error.message[3] === " ";
const body = hasBodyFormat && error.message.length > 3 ? error.message.slice(4) : undefined;
if (hasStatus && hasBodyFormat) fingerprint = ["{{ default }}", status];
if (hasStatus && hasBodyFormat && body !== undefined) {
try {
const json = JSON.parse(body) as { code?: unknown; error?: unknown; message?: unknown };
fingerprint = [
"{{ default }}",
status,
...("code" in json
? [String(json.code)]
: typeof json.error === "string"
? [json.error]
: typeof json.message === "string"
? [json.message]
: []),
];
} catch {
fingerprint = ["{{ default }}", status, body];
}
}
}
captureException(error, { level: "error", tags: { unhandled: true }, fingerprint });
return c.json({ code: "unexpected error", legacy: "unexpected error" }, 555 as UnofficialStatusCode);
});

Expand Down
32 changes: 16 additions & 16 deletions server/test/utils/manteca.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,17 @@ describe("manteca utils", () => {
});

it("returns null when user not found", async () => {
vi.spyOn(globalThis, "fetch").mockResolvedValueOnce(mockFetchError(404, "::404:: USER_NF"));
vi.spyOn(globalThis, "fetch").mockResolvedValueOnce(mockFetchError(404, "USER_NF"));

const result = await manteca.getUser(account);

expect(result).toBeNull();
});

it("throws on other errors", async () => {
vi.spyOn(globalThis, "fetch").mockResolvedValueOnce(mockFetchError(500, "::500:: internal error"));
vi.spyOn(globalThis, "fetch").mockResolvedValueOnce(mockFetchError(500, "internal error"));

await expect(manteca.getUser(account)).rejects.toThrow("::500:: internal error");
await expect(manteca.getUser(account)).rejects.toThrow("500 internal error");
});
});

Expand All @@ -134,7 +134,7 @@ describe("manteca utils", () => {
});

it("returns undefined on error", async () => {
vi.spyOn(globalThis, "fetch").mockResolvedValueOnce(mockFetchError(500, "::500:: error"));
vi.spyOn(globalThis, "fetch").mockResolvedValueOnce(mockFetchError(500, "error"));

const result = await manteca.getQuote("USDC_ARS");

Expand Down Expand Up @@ -172,7 +172,7 @@ describe("manteca utils", () => {
it("throws INVALID_ORDER_SIZE on MIN_SIZE error", async () => {
vi.spyOn(globalThis, "fetch")
.mockResolvedValueOnce(mockFetchResponse({ ...mockBalanceBase, balance: { ARS: "1.00" } }))
.mockResolvedValueOnce(mockFetchError(400, "::400:: MIN_SIZE"));
.mockResolvedValueOnce(mockFetchError(400, "MIN_SIZE"));

await expect(manteca.convertBalanceToUsdc("456", "ARS")).rejects.toThrow(ErrorCodes.INVALID_ORDER_SIZE);
});
Expand Down Expand Up @@ -238,7 +238,7 @@ describe("manteca utils", () => {
vi.spyOn(globalThis, "fetch").mockResolvedValueOnce({
ok: false,
status: 404,
text: () => Promise.resolve("::404:: USER_NF"),
text: () => Promise.resolve("USER_NF"),
} as Response);

const result = await manteca.getProvider(account, "AR");
Expand Down Expand Up @@ -284,7 +284,7 @@ describe("manteca utils", () => {
it("returns ACTIVE with no limits when limits fetch fails", async () => {
vi.spyOn(globalThis, "fetch")
.mockResolvedValueOnce(mockFetchResponse(mockActiveUser))
.mockResolvedValueOnce(mockFetchError(500, "::500:: limits error"));
.mockResolvedValueOnce(mockFetchError(500, "limits error"));

const result = await manteca.getProvider(account, "AR");

Expand Down Expand Up @@ -368,7 +368,7 @@ describe("manteca utils", () => {
vi.spyOn(globalThis, "fetch").mockResolvedValueOnce({
ok: false,
status: 404,
text: () => Promise.resolve("::404:: USER_NF"),
text: () => Promise.resolve("USER_NF"),
} as Response);

const result = await manteca.getProvider(account, "AR");
Expand Down Expand Up @@ -470,15 +470,15 @@ describe("manteca utils", () => {
});

it("throws when no persona account found", async () => {
vi.spyOn(globalThis, "fetch").mockResolvedValueOnce(mockFetchError(404, "::404:: USER_NF"));
vi.spyOn(globalThis, "fetch").mockResolvedValueOnce(mockFetchError(404, "USER_NF"));
vi.spyOn(persona, "getAccount").mockResolvedValueOnce(undefined); // eslint-disable-line unicorn/no-useless-undefined

await expect(manteca.mantecaOnboarding(account, credentialId)).rejects.toThrow(ErrorCodes.NO_PERSONA_ACCOUNT);
});

it("throws when no identity document found", async () => {
vi.spyOn(globalThis, "fetch")
.mockResolvedValueOnce(mockFetchError(404, "::404:: USER_NF"))
.mockResolvedValueOnce(mockFetchError(404, "USER_NF"))
.mockResolvedValueOnce(mockFetchResponse(mockNewUserResponse));
vi.spyOn(persona, "getAccount").mockResolvedValueOnce(mockPersonaAccount);
vi.spyOn(persona, "getDocumentForManteca").mockResolvedValueOnce(undefined); // eslint-disable-line unicorn/no-useless-undefined
Expand All @@ -488,7 +488,7 @@ describe("manteca utils", () => {

it("throws when front document URL not found", async () => {
vi.spyOn(globalThis, "fetch")
.mockResolvedValueOnce(mockFetchError(404, "::404:: USER_NF"))
.mockResolvedValueOnce(mockFetchError(404, "USER_NF"))
.mockResolvedValueOnce(mockFetchResponse(mockNewUserResponse));
vi.spyOn(persona, "getAccount").mockResolvedValueOnce(mockPersonaAccount);
vi.spyOn(persona, "getDocumentForManteca").mockResolvedValueOnce(mockIdentityDocument);
Expand All @@ -502,11 +502,11 @@ describe("manteca utils", () => {

it("throws INVALID_LEGAL_ID when initiateOnboarding returns legalId error", async () => {
vi.spyOn(globalThis, "fetch")
.mockResolvedValueOnce(mockFetchError(404, "::404:: USER_NF"))
.mockResolvedValueOnce(mockFetchError(404, "USER_NF"))
.mockResolvedValueOnce(
mockFetchError(
400,
'::400:: {"internalStatus":"BAD_REQUEST","message":"Bad request.","errors":["legalId has wrong value 20991231239"]}',
'{"internalStatus":"BAD_REQUEST","message":"Bad request.","errors":["legalId has wrong value 20991231239"]}',
),
);
vi.spyOn(persona, "getAccount").mockResolvedValueOnce(mockPersonaAccount);
Expand All @@ -517,7 +517,7 @@ describe("manteca utils", () => {

it("initiates onboarding for new user", async () => {
vi.spyOn(globalThis, "fetch")
.mockResolvedValueOnce(mockFetchError(404, "::404:: USER_NF"))
.mockResolvedValueOnce(mockFetchError(404, "USER_NF"))
.mockResolvedValueOnce(mockFetchResponse(mockNewUserResponse))
.mockResolvedValueOnce(mockFetchResponse({ url: "https://presigned.url/front" }))
.mockResolvedValueOnce(mockFetchResponse({ url: "https://presigned.url/back" }))
Expand Down Expand Up @@ -555,7 +555,7 @@ describe("manteca utils", () => {

const fetchSpy = vi
.spyOn(globalThis, "fetch")
.mockResolvedValueOnce(mockFetchError(404, "::404:: USER_NF"))
.mockResolvedValueOnce(mockFetchError(404, "USER_NF"))
.mockResolvedValueOnce(mockFetchResponse(mockNewUserResponse))
.mockResolvedValueOnce(mockFetchResponse({ url: "https://presigned.url/front" }))
.mockResolvedValueOnce(mockFetchResponse({ url: "https://presigned.url/back" }))
Expand Down Expand Up @@ -583,7 +583,7 @@ describe("manteca utils", () => {

const fetchSpy = vi
.spyOn(globalThis, "fetch")
.mockResolvedValueOnce(mockFetchError(404, "::404:: USER_NF"))
.mockResolvedValueOnce(mockFetchError(404, "USER_NF"))
.mockResolvedValueOnce(mockFetchResponse(mockNewUserResponse))
.mockResolvedValueOnce(mockFetchResponse({ url: "https://presigned.url/front" }))
.mockResolvedValueOnce(mockFetchResponse({ url: "https://presigned.url/back" }))
Expand Down
2 changes: 1 addition & 1 deletion server/utils/ramps/manteca.ts
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ async function request<TInput, TOutput, TIssue extends BaseIssue<unknown>>(
signal: AbortSignal.timeout(timeout),
});

if (!response.ok) throw new Error(`::${response.status}:: ${await response.text()}`);
if (!response.ok) throw new Error(`${response.status} ${await response.text()}`);
const rawBody = await response.arrayBuffer();
if (rawBody.byteLength === 0) return parse(schema, {});
return parse(schema, JSON.parse(new TextDecoder().decode(rawBody)));
Expand Down
2 changes: 1 addition & 1 deletion server/utils/sardine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ async function request<TInput, TOutput, TIssue extends BaseIssue<unknown>>(
signal: AbortSignal.timeout(timeout),
});

if (!response.ok) throw new Error(`${response.status} ${await response.text()} ${url}`);
if (!response.ok) throw new Error(`${response.status} ${await response.text()}`);
const rawBody = await response.arrayBuffer();
if (rawBody.byteLength === 0) throw new Error(`Empty response body from ${url}`);
return parse(schema, JSON.parse(new TextDecoder().decode(rawBody)));
Expand Down