mirror of
https://github.com/toeverything/AFFiNE.git
synced 2024-12-29 19:54:04 +03:00
test: throw no permission when download failed (#1742)
This commit is contained in:
parent
8a03f9ff1f
commit
9cd59d9146
@ -101,19 +101,20 @@ describe('api', () => {
|
||||
document.removeEventListener('affine-error', listener);
|
||||
});
|
||||
|
||||
test('blob too large', async () => {
|
||||
let called = false;
|
||||
try {
|
||||
await workspaceApis.uploadBlob(
|
||||
'test',
|
||||
new ArrayBuffer(1024 * 1024 * 1024 + 1),
|
||||
'image/png'
|
||||
);
|
||||
} catch (e) {
|
||||
called = true;
|
||||
test('no permission', async () => {
|
||||
await workspaceApis.downloadWorkspace('not-exist').catch(e => {
|
||||
expect(e).toBeInstanceOf(RequestError);
|
||||
}
|
||||
expect(called, 'throw error').toBe(true);
|
||||
expect(e.code).toBe(MessageCode.noPermission);
|
||||
});
|
||||
});
|
||||
|
||||
test('blob too large', async () => {
|
||||
await workspaceApis
|
||||
.uploadBlob('test', new ArrayBuffer(1024 * 1024 * 1024 + 1), 'image/png')
|
||||
.catch(e => {
|
||||
expect(e).toBeInstanceOf(RequestError);
|
||||
expect(e.code).toBe(MessageCode.blobTooLarge);
|
||||
});
|
||||
});
|
||||
|
||||
test('refresh token', async () => {
|
||||
|
@ -5,9 +5,12 @@ import { z } from 'zod';
|
||||
import { getLoginStorage } from '../login';
|
||||
|
||||
export class RequestError extends Error {
|
||||
public readonly code: MessageCode;
|
||||
|
||||
constructor(code: MessageCode, cause: unknown | null = null) {
|
||||
super(Messages[code].message);
|
||||
sendMessage(code);
|
||||
this.code = code;
|
||||
this.name = 'RequestError';
|
||||
this.cause = cause;
|
||||
}
|
||||
@ -360,8 +363,16 @@ export function createWorkspaceApis(prefixUrl = '/') {
|
||||
Authorization: auth.token,
|
||||
},
|
||||
})
|
||||
.then(r =>
|
||||
r.status === 403
|
||||
? Promise.reject(new RequestError(MessageCode.noPermission))
|
||||
: r
|
||||
)
|
||||
.then(r => r.arrayBuffer())
|
||||
.catch(e => {
|
||||
if (e instanceof RequestError) {
|
||||
throw e;
|
||||
}
|
||||
throw new RequestError(MessageCode.downloadWorkspaceFailed, e);
|
||||
});
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user