fix(server): convert error type to lower case (#10301)

This commit is contained in:
fengmk2
2025-02-20 03:04:21 +00:00
parent ba91b36cc3
commit e0b2b2b33c
2 changed files with 14 additions and 8 deletions
@@ -104,7 +104,7 @@ export class UserFriendlyError extends Error {
static fromUserFriendlyErrorJSON(body: UserFriendlyError) { static fromUserFriendlyErrorJSON(body: UserFriendlyError) {
return new UserFriendlyError( return new UserFriendlyError(
body.type as UserFriendlyErrorBaseType, body.type.toLowerCase() as UserFriendlyErrorBaseType,
body.name.toLowerCase() as keyof typeof USER_FRIENDLY_ERRORS, body.name.toLowerCase() as keyof typeof USER_FRIENDLY_ERRORS,
body.message, body.message,
body.data body.data
@@ -7,16 +7,17 @@ import { applyUpdate, Doc as YDoc } from 'yjs';
import { createTestingApp, type TestingApp } from '../../../__tests__/utils'; import { createTestingApp, type TestingApp } from '../../../__tests__/utils';
import { AppModule } from '../../../app.module'; import { AppModule } from '../../../app.module';
import { Config, InternalServerError } from '../../../base'; import { Config, UserFriendlyError } from '../../../base';
import { ConfigModule } from '../../../base/config'; import { ConfigModule } from '../../../base/config';
import { Models } from '../../../models'; import { Models } from '../../../models';
import { DocReader } from '..'; import { DatabaseDocReader, DocReader } from '..';
import { RpcDocReader } from '../reader'; import { RpcDocReader } from '../reader';
const test = ava as TestFn<{ const test = ava as TestFn<{
models: Models; models: Models;
app: TestingApp; app: TestingApp;
docReader: DocReader; docReader: DocReader;
databaseDocReader: DatabaseDocReader;
config: Config; config: Config;
}>; }>;
@@ -37,6 +38,7 @@ test.before(async t => {
t.context.models = app.get(Models); t.context.models = app.get(Models);
t.context.docReader = app.get(DocReader); t.context.docReader = app.get(DocReader);
t.context.databaseDocReader = app.get(DatabaseDocReader);
t.context.config = app.get(Config); t.context.config = app.get(Config);
t.context.app = app; t.context.app = app;
}); });
@@ -69,14 +71,18 @@ test('should return null when doc not found', async t => {
}); });
test('should throw error when doc service internal error', async t => { test('should throw error when doc service internal error', async t => {
const { docReader } = t.context; const { docReader, databaseDocReader } = t.context;
const docId = randomUUID(); const docId = randomUUID();
mock.method(docReader, 'getDoc', async () => { mock.method(databaseDocReader, 'getDoc', async () => {
throw new InternalServerError('mock doc service internal error'); throw new Error('mock doc service internal error');
}); });
await t.throwsAsync(docReader.getDoc(workspace.id, docId), { const err = await t.throwsAsync(docReader.getDoc(workspace.id, docId), {
instanceOf: InternalServerError, instanceOf: UserFriendlyError,
message: 'An internal error occurred.',
name: 'internal_server_error',
}); });
t.is(err.type, 'internal_server_error');
t.is(err.status, 500);
}); });
test('should fallback to database doc reader when endpoint network error', async t => { test('should fallback to database doc reader when endpoint network error', async t => {