Skip to content

Commit

Permalink
Merge pull request #88 from dipunm/error-handling
Browse files Browse the repository at this point in the history
Refactored `request` error handling
  • Loading branch information
gandlafbtc authored Jan 29, 2024
2 parents dec03df + fc93b5c commit 1788185
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
7 changes: 7 additions & 0 deletions src/model/Errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export class HttpResponseError extends Error {
status: number;
constructor(message: string, status: number) {
super(message);
this.status = status;
}
}
24 changes: 14 additions & 10 deletions src/request.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { checkResponse } from './utils';
import { HttpResponseError } from './model/Errors';

type RequestArgs = {
endpoint: string;
Expand All @@ -23,7 +23,7 @@ async function _request({
requestBody,
headers: requestHeaders,
...options
}: RequestOptions): Promise<Response> {
}: RequestOptions): Promise<unknown> {
const body = requestBody ? JSON.stringify(requestBody) : undefined;
const headers = {
...{ Accept: 'application/json, text/plain, */*' },
Expand All @@ -34,17 +34,21 @@ async function _request({
const response = await fetch(endpoint, { body, headers, ...options });

if (!response.ok) {
const { error, detail } = await response.json();
const message = error || detail || 'bad response';
throw new Error(message);
// expecting: { error: '', code: 0 }
// or: { detail: '' } (cashuBtc via pythonApi)
const { error, detail } = await response.json().catch(() => ({ error: 'bad response' }));
throw new HttpResponseError(error || detail || 'bad response', response.status);
}

return response;
try {
return await response.json();
} catch (err) {
console.error('Failed to parse HTTP response', err);
throw new HttpResponseError('bad response', response.status);
}
}

export default async function request<T>(options: RequestOptions): Promise<T> {
const response = await _request({ ...options, ...globalRequestOptions });
const data = await response.json().catch(() => ({ error: 'bad response' }));
checkResponse(data);
return data;
const data = await _request({ ...options, ...globalRequestOptions });
return data as T;
}
4 changes: 2 additions & 2 deletions test/wallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ describe('receive', () => {
});
test('test receive tokens already spent', async () => {
const msg = 'tokens already spent. Secret: oEpEuViVHUV2vQH81INUbq++Yv2w3u5H0LhaqXJKeR0=';
nock(mintUrl).post('/split').reply(200, { detail: msg });
nock(mintUrl).post('/split').reply(500, { detail: msg });
const wallet = new CashuWallet(mint);

const { tokensWithErrors } = await wallet.receive(tokenInput);
Expand All @@ -152,7 +152,7 @@ describe('receive', () => {
expect(/[A-Za-z0-9+/]{43}=/.test(t.token[0].proofs[0].secret)).toBe(true);
});
test('test receive could not verify proofs', async () => {
nock(mintUrl).post('/split').reply(200, { code: 0, error: 'could not verify proofs.' });
nock(mintUrl).post('/split').reply(500, { code: 0, error: 'could not verify proofs.' });
const wallet = new CashuWallet(mint);

const { tokensWithErrors } = await wallet.receive(tokenInput);
Expand Down

0 comments on commit 1788185

Please sign in to comment.