Skip to content
This repository was archived by the owner on May 12, 2026. It is now read-only.

Commit bce3edd

Browse files
committed
Consistent error reporting to front end (OBP API style)
1 parent c693a76 commit bce3edd

66 files changed

Lines changed: 218 additions & 560 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎src/lib/components/ConsentRequestCard.svelte‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
7575
if (!response.ok) {
7676
const errorData = await response.json();
77-
throw new Error(errorData.error || `Failed to create consent (HTTP ${response.status})`);
77+
throw new Error(errorData.message);
7878
}
7979
8080
const data = await response.json();

‎src/lib/components/MissingRoleAlert.svelte‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@
9898
if (!response.ok) {
9999
const errorData = await response.json();
100100
throw new Error(
101-
errorData.error || "Failed to submit entitlement request",
101+
errorData.message,
102102
);
103103
}
104104
}

‎src/lib/obp/errors.ts‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,28 @@ export function extractErrorDetails(err: unknown): {
2525
}
2626

2727
if (err && typeof err === "object") {
28-
// Try to extract message from object
29-
const message =
30-
(err as any).message ||
31-
(err as any).error ||
32-
(err as any).error_message ||
33-
JSON.stringify(err);
34-
return { message };
28+
if ((err as any).message) {
29+
return { message: (err as any).message };
30+
}
31+
return { message: JSON.stringify(err) };
3532
}
3633

3734
return { message: String(err) };
3835
}
3936

37+
/**
38+
* Build a JSON error response matching the OBP API ErrorMessage format:
39+
* { message: string, code: number } where code is the HTTP status code.
40+
*/
41+
export function obpErrorResponse(err: unknown, fallbackStatus: number = 500) {
42+
const { message } = extractErrorDetails(err);
43+
const status = (err as any)?.statusCode || fallbackStatus;
44+
return {
45+
body: { message, code: status },
46+
status: status as number,
47+
};
48+
}
49+
4050
export class OBPErrorBase extends Error {
4151
statusCode?: number;
4252

‎src/lib/obp/requests.ts‎

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,39 +12,22 @@ import { OBPErrorBase, OBPRequestError } from "$lib/obp/errors";
1212
*
1313
* IMPORTANT: Never simplify or hide OBP error messages - show them in full!
1414
*/
15+
/**
16+
* Extract error from OBP API response.
17+
* OBP returns: { message: string, code: number }
18+
* If the format doesn't match, surface what we got so the problem is visible.
19+
*/
1520
function extractOBPError(
1621
data: any,
1722
response: Response,
1823
): { code?: string; message: string } {
19-
// If data is a string, return it directly
20-
if (typeof data === "string") {
21-
return {
22-
message: data || `HTTP ${response.status}: ${response.statusText}`,
23-
};
24-
}
25-
26-
// If data is not an object, return status info
27-
if (!data || typeof data !== "object") {
28-
return { message: `HTTP ${response.status}: ${response.statusText}` };
24+
if (data && typeof data === "object" && typeof data.message === "string") {
25+
return { code: data.code != null ? String(data.code) : undefined, message: data.message };
2926
}
3027

31-
// Extract code from various possible fields
32-
const code = data.code || data.failCode || data.error_code;
33-
34-
// Extract message from various possible fields - NEVER use generic fallback if we have data!
35-
const message =
36-
data.message ||
37-
data.failMsg ||
38-
data.error ||
39-
data.error_message ||
40-
// If we have a code but no message, at least show the code
41-
(code ? `Error code: ${code}` : null) ||
42-
// If we have any other data, stringify it to show the full response
43-
(Object.keys(data).length > 0
44-
? JSON.stringify(data)
45-
: `HTTP ${response.status}: ${response.statusText}`);
46-
47-
return { code, message };
28+
throw new Error(
29+
`Unexpected error format from OBP API (HTTP ${response.status}): ${JSON.stringify(data)}`,
30+
);
4831
}
4932

5033
class OBPRequests {

‎src/routes/(protected)/abac/rules/[rule_id]/test/+page.svelte‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@
106106
107107
if (!response.ok) {
108108
const errorData = await response.json();
109-
throw new Error(errorData.error || "Failed to execute ABAC rule");
109+
throw new Error(errorData.message);
110110
}
111111
112112
const result = await response.json();

‎src/routes/(protected)/account-access/custom-views/create/+page.svelte‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@
163163
164164
if (!response.ok) {
165165
const errorData = await response.json();
166-
throw new Error(errorData.error || "Failed to create custom view");
166+
throw new Error(errorData.message);
167167
}
168168
169169
const result = await response.json();

‎src/routes/(protected)/api-collections/[collection_id]/+page.svelte‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
5656
if (!response.ok) {
5757
const errorData = await response.json();
58-
throw new Error(errorData.error || "Failed to add endpoint");
58+
throw new Error(errorData.message);
5959
}
6060
6161
toast.success("Endpoint Added", `Added ${newOperationId} to collection`);

‎src/routes/(protected)/api-collections/[collection_id]/delete/+page.svelte‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
3636
if (!response.ok) {
3737
const errorData = await response.json();
38-
throw new Error(errorData.error || "Failed to delete collection");
38+
throw new Error(errorData.message);
3939
}
4040
4141
toast.success(

‎src/routes/(protected)/api-collections/[collection_id]/edit/+page.svelte‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
5050
if (!response.ok) {
5151
const errorData = await response.json();
52-
throw new Error(errorData.error || "Failed to update collection");
52+
throw new Error(errorData.message);
5353
}
5454
5555
toast.success(

‎src/routes/(protected)/api-collections/create/+page.svelte‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
4444
if (!response.ok) {
4545
const errorData = await response.json();
46-
throw new Error(errorData.error || "Failed to create collection");
46+
throw new Error(errorData.message);
4747
}
4848
4949
toast.success(

0 commit comments

Comments
 (0)