Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

Feat/pop 34 convert error code #12

Merged
merged 8 commits into from
Mar 12, 2024
33 changes: 33 additions & 0 deletions src/entry/adaptor/errors.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { describe, it, expect } from 'vitest';

describe('正しくエラーコードに変換できる', () => {
const errorToCode = (error: Error) => {
switch (error.message) {
case 'too many members':
return 'TOO_MANY_MEMBERS';
case 'no member':
return 'NO_MEMBER';
case 'teamName Exists':
return 'TEAM_ALREADY_EXISTS';
default:
return 'UNKNOWN_ERROR';
}
};
it('too many members', () => {
const error = new Error('too many members');
expect(errorToCode(error)).toBe('TOO_MANY_MEMBERS');
});
it('no member', () => {
const error = new Error('no member');
expect(errorToCode(error)).toBe('NO_MEMBER');
});
it('teamName Exists', () => {
const error = new Error('teamName Exists');
expect(errorToCode(error)).toBe('TEAM_ALREADY_EXISTS');
});

it('other', () => {
const error = new Error('other');
expect(errorToCode(error)).toBe('UNKNOWN_ERROR');
});
});
12 changes: 12 additions & 0 deletions src/entry/adaptor/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export function errorToCode(error: Error): string {
switch (error.message) {
case 'too many members':
return 'TOO_MANY_MEMBERS';
case 'no member':
return 'NO_MEMBER';
case 'teamName Exists':
return 'TEAM_ALREADY_EXISTS';
default:
return 'UNKNOWN_ERROR';
}
}
5 changes: 3 additions & 2 deletions src/entry/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { entryRequestSchema } from './schema.js';
import { Controller } from './controller.js';
import { Option, Result } from '@mikuroxina/mini-fn';
import { JSONEntryRepository } from './adaptor/json.js';
import { errorToCode } from './adaptor/errors.js';

export const entryHandler = new Hono();
// export const controller = new Controller(new DummyRepository());
Expand All @@ -19,7 +20,7 @@ entryHandler.post('/', zValidator('json', entryRequestSchema), async (c) => {
category,
});
if (Result.isErr(res)) {
return c.json({ error: res[1].message }, 400);
return c.json({ error: errorToCode(res[1]) }, 400);
}

return c.json({
Expand All @@ -34,7 +35,7 @@ entryHandler.post('/', zValidator('json', entryRequestSchema), async (c) => {
entryHandler.get('/', async (c) => {
const res = await controller.get();
if (Result.isErr(res)) {
return c.json({ error: res[1].message }, 400);
return c.json({ error: errorToCode(res[1]) }, 400);
}

return c.json([...res[1]]);
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"moduleResolution": "Node16",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"pretty": true
"pretty": true,
},
"include": ["**/*.ts"],
"exclude": ["node_modules"]
Expand Down
Loading