Skip to content

Commit 6a485e4

Browse files
committed
fixed error handling
1 parent 9d4fd5e commit 6a485e4

File tree

7 files changed

+160
-113
lines changed

7 files changed

+160
-113
lines changed

jsr.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@mycore-test/js-common",
3-
"version": "0.1.26",
3+
"version": "0.1.28",
44
"exports": {
55
"./i18n": "./src/i18n/index.ts",
66
"./access-key": "./src/access-key/index.ts",

src/access-key/access-key.service.ts

Lines changed: 51 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*/
1818

1919
import { AuthStrategy } from '../auth';
20-
import { handleError } from '../utils/http';
20+
import { handleError, ensureOk } from '../utils/http';
2121
import {
2222
AccessKey,
2323
CreateAccessKey,
@@ -51,6 +51,23 @@ export interface GetAccessKeysOptions {
5151
limit?: number;
5252
}
5353

54+
const buildSearchParams = (options?: GetAccessKeysOptions): string => {
55+
const searchParams = new URLSearchParams();
56+
if (options?.reference) {
57+
searchParams.set('reference', options.reference);
58+
}
59+
if (options?.permissions?.length) {
60+
searchParams.set('permissions', options.permissions.join(','));
61+
}
62+
if (options?.offset) {
63+
searchParams.set('offset', String(options.offset));
64+
}
65+
if (options?.limit) {
66+
searchParams.set('limit', String(options.limit));
67+
}
68+
return searchParams.toString();
69+
};
70+
5471
/**
5572
* Base path for access key resource endpoint.
5673
*/
@@ -76,40 +93,29 @@ export class AccessKeyService {
7693
* @returns A promise that resolves with the access keys information.
7794
* @throws {UnauthorizedActionError} If the user is not authorized.
7895
* @throws {PermissionError} If the user is not allowed to request access keys.
96+
* @throws {Error} If an unknown error occurred.
7997
*/
8098
public async getAccessKeys(
8199
options?: GetAccessKeysOptions
82100
): Promise<AccessKeysSummary> {
83-
const searchParams = new URLSearchParams();
84-
if (options?.reference) {
85-
searchParams.set('reference', options.reference);
86-
}
87-
if (options?.permissions && options.permissions.length > 0) {
88-
searchParams.set('permissions', options.permissions.join(','));
89-
}
90-
if (options?.offset) {
91-
searchParams.set('offset', String(options.offset));
92-
}
93-
if (options?.limit) {
94-
searchParams.set('limit', String(options.limit));
95-
}
96101
try {
97-
const response = await fetch(this.getUrl(`?${searchParams.toString()}`), {
98-
headers: {
99-
...this.getAuthHeaders(),
100-
Accept: 'application/json',
101-
},
102-
});
103-
if (!response.ok) {
104-
handleError(response);
105-
}
102+
const response = await fetch(
103+
this.getUrl(`?${buildSearchParams(options)}`),
104+
{
105+
headers: {
106+
...this.getAuthHeaders(),
107+
Accept: 'application/json',
108+
},
109+
}
110+
);
111+
ensureOk(response);
106112
const totalCount = response.headers.get('x-total-count');
107113
return {
108114
accessKeys: (await response.json()) as AccessKey[],
109115
totalCount: totalCount ? parseInt(totalCount, 10) : 0,
110116
} as AccessKeysSummary;
111-
} catch {
112-
throw new Error('Failed to get access keys');
117+
} catch (error) {
118+
throw handleError(error, 'Failed to get access keys');
113119
}
114120
}
115121

@@ -120,6 +126,7 @@ export class AccessKeyService {
120126
* @throws {UnauthorizedActionError} If the user is not authorized.
121127
* @throws {PermissionError} If the user is not allowed to request access keys.
122128
* @throws {ResourceNotFoundError} If the access key does not exist.
129+
* @throws {Error} If an unknown error occurred.
123130
*/
124131
public async getAccessKey(id: string): Promise<AccessKey> {
125132
try {
@@ -129,12 +136,10 @@ export class AccessKeyService {
129136
Accept: 'application/json',
130137
},
131138
});
132-
if (!response.ok) {
133-
handleError(response);
134-
}
139+
ensureOk(response);
135140
return (await response.json()) as AccessKey;
136-
} catch {
137-
throw new Error('Failed to get access key');
141+
} catch (error) {
142+
throw handleError(error, 'Failed to get access key');
138143
}
139144
}
140145

@@ -144,6 +149,7 @@ export class AccessKeyService {
144149
* @returns A promise that resolves with the ID of the created access key.
145150
* @throws {UnauthorizedActionError} If the user is not authorized.
146151
* @throws {PermissionError} If the user is not allowed to create access key.
152+
* @throws {Error} If an unknown error occurred.
147153
*/
148154
public async createAccessKey(accessKey: CreateAccessKey): Promise<string> {
149155
try {
@@ -155,12 +161,10 @@ export class AccessKeyService {
155161
},
156162
body: JSON.stringify(accessKey),
157163
});
158-
if (!response.ok) {
159-
handleError(response);
160-
}
164+
ensureOk(response);
161165
return response.headers.get('location')?.split('/').pop() as string;
162-
} catch {
163-
throw new Error('Failed to create access key');
166+
} catch (error) {
167+
throw handleError(error, 'Failed to create access key');
164168
}
165169
}
166170

@@ -171,6 +175,7 @@ export class AccessKeyService {
171175
* @throws {UnauthorizedActionError} If the user is not authorized.
172176
* @throws {PermissionError} If the user is not allowed to update access key.
173177
* @throws {ResourceNotFoundError} If the access key does not exist.
178+
* @throws {Error} If an unknown error occurred.
174179
*/
175180
public async updateAccessKey(
176181
id: string,
@@ -185,11 +190,9 @@ export class AccessKeyService {
185190
},
186191
body: JSON.stringify(accessKey),
187192
});
188-
if (!response.ok) {
189-
handleError(response);
190-
}
191-
} catch {
192-
throw new Error('Failed to update access key');
193+
ensureOk(response);
194+
} catch (error) {
195+
throw handleError(error, 'Failed to update access key');
193196
}
194197
}
195198

@@ -200,6 +203,7 @@ export class AccessKeyService {
200203
* @throws {UnauthorizedActionError} If the user is not authorized.
201204
* @throws {PermissionError} If the user is not allowed to update access key.
202205
* @throws {ResourceNotFoundError} If the access key does not exist.
206+
* @throws {Error} If an unknown error occurred.
203207
*/
204208
public async patchAccessKey(
205209
id: string,
@@ -214,11 +218,9 @@ export class AccessKeyService {
214218
},
215219
body: JSON.stringify(accessKey),
216220
});
217-
if (!response.ok) {
218-
handleError(response);
219-
}
220-
} catch {
221-
throw new Error('Failed to update access key');
221+
ensureOk(response);
222+
} catch (error) {
223+
throw handleError(error, 'Failed to update access key');
222224
}
223225
}
224226

@@ -228,6 +230,7 @@ export class AccessKeyService {
228230
* @throws {UnauthorizedActionError} If the user is not authorized.
229231
* @throws {PermissionError} If the user is not allowed to delete access key.
230232
* @throws {ResourceNotFoundError} If the access key does not exist.
233+
* @throws {Error} If an unknown error occurred.
231234
*/
232235
public async deleteAccessKey(id: string): Promise<void> {
233236
try {
@@ -237,11 +240,9 @@ export class AccessKeyService {
237240
...this.getAuthHeaders(),
238241
},
239242
});
240-
if (!response.ok) {
241-
handleError(response);
242-
}
243-
} catch {
244-
throw new Error('Failed to delete access key');
243+
ensureOk(response);
244+
} catch (error) {
245+
throw handleError(error, 'Failed to delete access key');
245246
}
246247
}
247248

src/orcid/orcid-user.service.ts

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*/
1818

1919
import { AuthStrategy } from '../auth/types.ts';
20-
import { handleError } from '../utils/http/index.ts';
20+
import { handleError, ensureOk } from '../utils/http';
2121
import { OrcidUserSettings, OrcidUserStatus } from './types.ts';
2222

2323
/**
@@ -47,6 +47,7 @@ export class OrcidUserService {
4747
* @throws An error if the request fails or the status cannot be retrieved.
4848
* @throws {UnauthorizedActionError} If the user is not authorized.
4949
* @throws {PermissionError} If the user is not allowed to get user status.
50+
* @throws {Error} If an unknown error occurred.
5051
*/
5152
public getUserStatus = async (): Promise<OrcidUserStatus> => {
5253
try {
@@ -56,12 +57,10 @@ export class OrcidUserService {
5657
Accept: 'application/json',
5758
},
5859
});
59-
if (!response.ok) {
60-
handleError(response);
61-
}
60+
ensureOk(response);
6261
return (await response.json()) as OrcidUserStatus;
63-
} catch {
64-
throw new Error('Failed to fetch Orcid user status');
62+
} catch (error) {
63+
throw handleError(error, 'Failed to fetch Orcid user status');
6564
}
6665
};
6766

@@ -71,6 +70,7 @@ export class OrcidUserService {
7170
* @returns A promise that resolves when the authentication is successfully revoked.
7271
* @throws {UnauthorizedActionError} If the user is not authorized.
7372
* @throws {PermissionError} If the user is not allowed to revoke ORCID.
73+
* @throws {Error} If an unknown error occurred.
7474
*/
7575
public revokeAuth = async (orcid: string): Promise<void> => {
7676
try {
@@ -80,11 +80,9 @@ export class OrcidUserService {
8080
...this.getAuthHeaders(),
8181
},
8282
});
83-
if (!response.ok) {
84-
handleError(response);
85-
}
86-
} catch {
87-
throw new Error('Failed to revoke ORCID');
83+
ensureOk(response);
84+
} catch (error) {
85+
throw handleError(error, 'Failed to revoke ORCID');
8886
}
8987
};
9088

@@ -96,6 +94,7 @@ export class OrcidUserService {
9694
* @throws An error if the request fails or the settings cannot be retrieved.
9795
* @throws {UnauthorizedActionError} If the user is not authorized.
9896
* @throws {PermissionError} If the user is not allowed to get user settings.
97+
* @throws {Error} If an unknown error occurred.
9998
*/
10099
public getUserSettings = async (
101100
orcid: string
@@ -107,12 +106,13 @@ export class OrcidUserService {
107106
Accept: 'application/json',
108107
},
109108
});
110-
if (!response.ok) {
111-
handleError(response);
112-
}
109+
ensureOk(response);
113110
return (await response.json()) as OrcidUserSettings;
114-
} catch {
115-
throw new Error(`Failed to fetch ORCID user settings for ${orcid}.`);
111+
} catch (error) {
112+
throw handleError(
113+
error,
114+
`Failed to fetch ORCID user settings for ${orcid}.`
115+
);
116116
}
117117
};
118118

@@ -125,6 +125,7 @@ export class OrcidUserService {
125125
* @throws An error if the request fails or the settings cannot be updated.
126126
* @throws {UnauthorizedActionError} If the user is not authorized.
127127
* @throws {PermissionError} If the user is not allowed to update user settings.
128+
* @throws {Error} If an unknown error occurred.
128129
*/
129130
public updateUserSettings = async (
130131
orcid: string,
@@ -139,11 +140,12 @@ export class OrcidUserService {
139140
},
140141
body: JSON.stringify(settings),
141142
});
142-
if (!response.ok) {
143-
handleError(response);
144-
}
145-
} catch {
146-
throw new Error(`Failed to update ORCID user settings for ${orcid}.`);
143+
ensureOk(response);
144+
} catch (error) {
145+
throw handleError(
146+
error,
147+
`Failed to update ORCID user settings for ${orcid}.`
148+
);
147149
}
148150
};
149151

src/orcid/orcid-work.service.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*/
1818

1919
import { AuthStrategy } from '../auth';
20-
import { handleError } from '../utils/http';
20+
import { handleError, ensureOk } from '../utils/http';
2121
import { OrcidWorkStatus } from './types';
2222

2323
/**
@@ -53,6 +53,7 @@ export class OrcidWorkService {
5353
* @returns A promise that resolves to an `OrcidWorkStatusDto` object containing the status of the work
5454
* @throws {UnauthorizedActionError} If the user is not authorized.
5555
* @throws {PermissionError} If the user is not allowed to get work status.
56+
* @throws {Error} If an unknown error occurred.
5657
*/
5758
public getWorkStatus = async (
5859
orcid: string,
@@ -68,12 +69,10 @@ export class OrcidWorkService {
6869
},
6970
}
7071
);
71-
if (!response.ok) {
72-
handleError(response);
73-
}
72+
ensureOk(response);
7473
return (await response.json()) as OrcidWorkStatus;
75-
} catch {
76-
throw new Error(`Failed to fetch work status for ${objectId}.`);
74+
} catch (error) {
75+
throw handleError(error, `Failed to fetch work status for ${objectId}.`);
7776
}
7877
};
7978

@@ -86,6 +85,7 @@ export class OrcidWorkService {
8685
* @throws {UnauthorizedActionError} If the user is not authorized.
8786
* @throws {PermissionError} If the user is not allowed to export object.
8887
* @throws {ResourceNotFoundError} If the object does not exist.
88+
* @throws {Error} If an unknown error occurred.
8989
*/
9090
public exportObject = async (
9191
orcid: string,
@@ -98,11 +98,9 @@ export class OrcidWorkService {
9898
method: 'POST',
9999
}
100100
);
101-
if (!response.ok) {
102-
handleError(response);
103-
}
104-
} catch {
105-
throw new Error(`Failed to export ${objectId} to ${orcid}.`);
101+
ensureOk(response);
102+
} catch (error) {
103+
throw handleError(error, `Failed to export ${objectId} to ${orcid}.`);
106104
}
107105
};
108106

0 commit comments

Comments
 (0)