test: throw no permission when download failed (#1742)

This commit is contained in:
Himself65 2023-03-29 13:42:55 -05:00 committed by GitHub
parent 8a03f9ff1f
commit 9cd59d9146
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 12 deletions

View File

@ -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 () => {

View File

@ -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);
});
}