Skip to content

Commit 47dd2b9

Browse files
committed
debug api
1 parent db3772f commit 47dd2b9

File tree

2 files changed

+44
-11
lines changed

2 files changed

+44
-11
lines changed

src/api.ts

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,22 +75,37 @@ export const closeSession = () => {
7575
session = undefined;
7676
};
7777

78+
function createRequestError(error: unknown, requestUrl: string) {
79+
const message =
80+
typeof error === 'string'
81+
? error
82+
: error instanceof Error
83+
? error.message
84+
: String(error);
85+
return new Error(`${message}\nURL: ${requestUrl}`);
86+
}
87+
7888
async function query(url: string, options: fetch.RequestInit) {
7989
const baseUrl = await getBaseUrl;
8090
const fullUrl = `${baseUrl}${url}`;
81-
const resp = await fetch(fullUrl, options);
91+
let resp: fetch.Response;
92+
try {
93+
resp = await fetch(fullUrl, options);
94+
} catch (error) {
95+
throw createRequestError(error, fullUrl);
96+
}
8297
const text = await resp.text();
8398
let json: any;
8499
try {
85100
json = JSON.parse(text);
86101
} catch (e) {}
87102

88103
if (resp.status !== 200) {
89-
const message = json?.message || resp.statusText;
104+
const message = json?.message || resp.statusText || `HTTP ${resp.status}`;
90105
if (resp.status === 401) {
91-
throw new Error(t('loginExpired'));
106+
throw createRequestError(t('loginExpired'), fullUrl);
92107
}
93-
throw new Error(message);
108+
throw createRequestError(message, fullUrl);
94109
}
95110
return json;
96111
}
@@ -195,13 +210,21 @@ export async function uploadFile(fn: string, key?: string) {
195210
// contentType: 'application/octet-stream',
196211
// });
197212

198-
const res = await fetch(realUrl, {
199-
method: 'POST',
200-
body: form,
201-
});
213+
let res: fetch.Response;
214+
try {
215+
res = await fetch(realUrl, {
216+
method: 'POST',
217+
body: form,
218+
});
219+
} catch (error) {
220+
throw createRequestError(error, realUrl);
221+
}
202222

203223
if (res.status > 299) {
204-
throw new Error(`${res.status}: ${res.statusText}`);
224+
throw createRequestError(
225+
`${res.status}: ${res.statusText || 'Upload failed'}`,
226+
realUrl,
227+
);
205228
}
206229

207230
// const body = await response.json();

tests/api.test.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
import { describe, expect, test, mock, afterEach, beforeEach, spyOn } from 'bun:test';
1+
import {
2+
afterEach,
3+
beforeEach,
4+
describe,
5+
expect,
6+
mock,
7+
spyOn,
8+
test,
9+
} from 'bun:test';
210
import fs from 'fs';
311
import { loadSession } from '../src/api';
412

@@ -13,7 +21,9 @@ describe('api.ts loadSession', () => {
1321

1422
// Use spyOn to mock specific fs methods
1523
existsSyncSpy = spyOn(fs, 'existsSync').mockReturnValue(true);
16-
readFileSyncSpy = spyOn(fs, 'readFileSync').mockReturnValue('{ "invalid": json ');
24+
readFileSyncSpy = spyOn(fs, 'readFileSync').mockReturnValue(
25+
'{ "invalid": json ',
26+
);
1727
});
1828

1929
afterEach(() => {

0 commit comments

Comments
 (0)