Skip to content

Commit

Permalink
Merge pull request #26 from EATSTEAK/fix/error
Browse files Browse the repository at this point in the history
모든 응답이 올바른 오류를 표시하도록 수정
  • Loading branch information
EATSTEAK authored Aug 13, 2022
2 parents e04c7d5 + 8154547 commit 643c101
Show file tree
Hide file tree
Showing 15 changed files with 85 additions and 72 deletions.
6 changes: 3 additions & 3 deletions packages/server/src/auth/handler/logout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as jwt from 'jsonwebtoken';
import { JWT_SECRET } from '../../env';
import { assertAccessible, revokeToken } from '../data';
import { createResponse } from '../../common';
import { ResponsibleError } from '../../util/error';
import { errorResponse, isResponsibleError, ResponsibleError } from '../../util/error';

export const logoutHandler: APIGatewayProxyHandler = async (event) => {
const token = (event.headers.Authorization ?? '').replace('Bearer ', '');
Expand All @@ -14,8 +14,8 @@ export const logoutHandler: APIGatewayProxyHandler = async (event) => {
const res = await revokeToken(payload.aud as string, token);
return createResponse(200, { success: true, ...res });
} catch (err) {
if (err instanceof ResponsibleError) {
return err.response();
if (isResponsibleError(err)) {
return errorResponse(err as ResponsibleError);
}
const res = {
success: false,
Expand Down
8 changes: 4 additions & 4 deletions packages/server/src/auth/handler/ssu_login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { APIGatewayProxyHandler } from 'aws-lambda';
import * as jwt from 'jsonwebtoken';
import { JWT_SECRET } from '../../env';
import { createResponse } from '../../common';
import { ResponsibleError, UnauthorizedError } from '../../util/error';
import { errorResponse, isResponsibleError, ResponsibleError, UnauthorizedError } from '../../util/error';
import { issueToken } from '../data';
import { queryConfig } from '../../config/data';
import { adminId } from '../../util/database';
Expand Down Expand Up @@ -70,9 +70,9 @@ export const ssuLoginHandler: APIGatewayProxyHandler = async (event) => {
};
return createResponse(200, { success: true, ...res });
}
return new UnauthorizedError('Unauthorized').response();
return errorResponse(new UnauthorizedError('Unauthorized'));
} catch (e) {
if (!(e instanceof ResponsibleError)) {
if (!(isResponsibleError(e))) {
console.error(e);
const res = {
success: false,
Expand All @@ -81,6 +81,6 @@ export const ssuLoginHandler: APIGatewayProxyHandler = async (event) => {
};
return createResponse(500, res);
}
return e.response();
return errorResponse(e as ResponsibleError);
}
};
6 changes: 3 additions & 3 deletions packages/server/src/config/handler/get.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { APIGatewayProxyHandler } from 'aws-lambda';
import { createResponse } from '../../common';
import { queryConfig } from '../data';
import { ResponsibleError } from '../../util/error';
import { errorResponse, isResponsibleError, ResponsibleError } from '../../util/error';

export const getConfigHandler: APIGatewayProxyHandler = async (event) => {
try {
Expand All @@ -11,8 +11,8 @@ export const getConfigHandler: APIGatewayProxyHandler = async (event) => {
result: configs
});
} catch (e) {
if (e instanceof ResponsibleError) {
return e.response();
if (isResponsibleError(e)) {
return errorResponse(e as ResponsibleError);
}
return createResponse(200, {
success: false,
Expand Down
6 changes: 3 additions & 3 deletions packages/server/src/config/handler/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import * as jwt from 'jsonwebtoken';
import { JWT_SECRET } from '../../env';
import { createResponse } from '../../common';
import { assertAccessible } from '../../auth/data';
import { ResponsibleError } from '../../util/error';
import { updateConfig } from '../data';
import { errorResponse, isResponsibleError, ResponsibleError } from '../../util/error';

export const updateConfigHandler: APIGatewayProxyHandler = async (event) => {
const token = (event.headers.Authorization ?? '').replace('Bearer ', '');
Expand Down Expand Up @@ -43,8 +43,8 @@ export const updateConfigHandler: APIGatewayProxyHandler = async (event) => {
await updateConfig(data);
return createResponse(200, { success: true });
} catch (e) {
if (e instanceof ResponsibleError) {
return e.response();
if (isResponsibleError(e)) {
return errorResponse(e as ResponsibleError);
}
console.error(e);
const res = {
Expand Down
6 changes: 3 additions & 3 deletions packages/server/src/locker/handler/claim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import type { JwtPayload } from 'jsonwebtoken';
import * as jwt from 'jsonwebtoken';
import { JWT_SECRET } from '../../env';
import { claimLocker } from '../data';
import { ResponsibleError } from '../../util/error';
import { getUser } from '../../user/data';
import { isValidLocker } from '../../util/locker';
import { getConfig } from '../../config/data';
import { errorResponse, isResponsibleError, ResponsibleError } from '../../util/error';

export const claimLockerHandler: APIGatewayProxyHandler = async (event) => {
const token = (event.headers.Authorization ?? '').replace('Bearer ', '');
Expand Down Expand Up @@ -59,7 +59,7 @@ export const claimLockerHandler: APIGatewayProxyHandler = async (event) => {
: await claimLocker(id, token, lockerId);
return createResponse(200, { success: true, result: res });
} catch (e) {
if (!(e instanceof ResponsibleError)) {
if (!(isResponsibleError(e))) {
console.error(e);
const res = {
success: false,
Expand All @@ -68,6 +68,6 @@ export const claimLockerHandler: APIGatewayProxyHandler = async (event) => {
};
return createResponse(500, res);
}
return e.response();
return errorResponse(e as ResponsibleError);
}
};
6 changes: 3 additions & 3 deletions packages/server/src/locker/handler/unclaim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { JwtPayload } from 'jsonwebtoken';
import * as jwt from 'jsonwebtoken';
import { JWT_SECRET } from '../../env';
import { unclaimLocker } from '../data';
import { ResponsibleError } from '../../util/error';
import { errorResponse, isResponsibleError, ResponsibleError } from '../../util/error';

export const unclaimLockerHandler: APIGatewayProxyHandler = async (event) => {
const token = (event.headers.Authorization ?? '').replace('Bearer ', '');
Expand All @@ -15,7 +15,7 @@ export const unclaimLockerHandler: APIGatewayProxyHandler = async (event) => {
const res = await unclaimLocker(id, token);
return createResponse(200, { success: true, result: res });
} catch (e) {
if (!(e instanceof ResponsibleError)) {
if (!(isResponsibleError(e))) {
console.error(e);
const res = {
success: false,
Expand All @@ -24,6 +24,6 @@ export const unclaimLockerHandler: APIGatewayProxyHandler = async (event) => {
};
return createResponse(500, res);
}
return e.response();
return errorResponse(e as ResponsibleError);
}
};
4 changes: 2 additions & 2 deletions packages/server/src/user/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export const deleteUser = async function(id: string): Promise<string> {
};
export const batchPutUser = async function(infos: Array<User>): Promise<Array<User>> {
if (infos.length === 0) return infos;
if (infos.length > 25) throw new ResponsibleError('Maximum amount of batch creation is 25');
if (infos.length > 25) throw new ResponsibleError(500, 'Maximum amount of batch creation is 25');
const requests: WriteRequest[] = infos.map((v: User) => ({
PutRequest: {
Item: {
Expand All @@ -145,7 +145,7 @@ export const batchPutUser = async function(infos: Array<User>): Promise<Array<Us

export const batchDeleteUser = async function(ids: Array<string>): Promise<Array<string>> {
if (ids.length === 0) return ids;
if (ids.length > 25) throw new ResponsibleError('Maximum amount of batch creation is 25');
if (ids.length > 25) throw new ResponsibleError(500, 'Maximum amount of batch creation is 25');
const requests: WriteRequest[] = ids.map((v: string) => ({
DeleteRequest: {
Key: {
Expand Down
8 changes: 4 additions & 4 deletions packages/server/src/user/handler/batch/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { JWT_SECRET } from '../../../env';
import { createResponse } from '../../../common';
import { assertAccessible } from '../../../auth/data';
import { batchDeleteUser } from '../../data';
import { ResponsibleError } from '../../../util/error';
import { errorResponse, isResponsibleError, ResponsibleError } from '../../../util/error';

export const batchDeleteUserHandler: APIGatewayProxyHandler = async (event) => {
const token = (event.headers.Authorization ?? '').replace('Bearer ', '');
Expand Down Expand Up @@ -47,9 +47,9 @@ export const batchDeleteUserHandler: APIGatewayProxyHandler = async (event) => {
}
return createResponse(200, { success: true });
} catch (e) {
if (e instanceof ResponsibleError) {
e.additionalInfo.failed_data = data.slice(i, data.length);
return e.response();
if (isResponsibleError(e)) {
(e as ResponsibleError).additionalInfo.failed_data = data.slice(i, data.length);
return errorResponse(e as ResponsibleError);
}
console.error(e);
const res = {
Expand Down
8 changes: 4 additions & 4 deletions packages/server/src/user/handler/batch/put.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { JWT_SECRET } from '../../../env';
import { createResponse } from '../../../common';
import { assertAccessible } from '../../../auth/data';
import { batchPutUser } from '../../data';
import { ResponsibleError } from '../../../util/error';
import { errorResponse, isResponsibleError, ResponsibleError } from '../../../util/error';

export const batchPutUserHandler: APIGatewayProxyHandler = async (event) => {
const token = (event.headers.Authorization ?? '').replace('Bearer ', '');
Expand Down Expand Up @@ -47,9 +47,9 @@ export const batchPutUserHandler: APIGatewayProxyHandler = async (event) => {
}
return createResponse(200, { success: true });
} catch (e) {
if (e instanceof ResponsibleError) {
e.additionalInfo.failed_data = data.slice(i, data.length);
return e.response();
if (isResponsibleError(e)) {
(e as ResponsibleError).additionalInfo.failed_data = data.slice(i, data.length);
return errorResponse(e as ResponsibleError);
}
console.error(e);
const res = {
Expand Down
6 changes: 3 additions & 3 deletions packages/server/src/user/handler/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { JWT_SECRET } from '../../env';
import { createResponse } from '../../common';
import { assertAccessible } from '../../auth/data';
import { deleteUser } from '../data';
import { ResponsibleError } from '../../util/error';
import { errorResponse, isResponsibleError, ResponsibleError } from '../../util/error';

export const deleteUserHandler: APIGatewayProxyHandler = async (event) => {
const token = (event.headers.Authorization ?? '').replace('Bearer ', '');
Expand Down Expand Up @@ -43,8 +43,8 @@ export const deleteUserHandler: APIGatewayProxyHandler = async (event) => {
const res = await deleteUser(data.id);
return createResponse(200, { success: true, result: res });
} catch (e) {
if (e instanceof ResponsibleError) {
return e.response();
if (isResponsibleError(e)) {
return errorResponse(e as ResponsibleError);
}
console.error(e);
const res = {
Expand Down
6 changes: 3 additions & 3 deletions packages/server/src/user/handler/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { JsonWebTokenError, TokenExpiredError } from 'jsonwebtoken';
import { JWT_SECRET } from '../../env';
import { createResponse } from '../../common';
import { getUser } from '../data';
import { ResponsibleError } from '../../util/error';
import { errorResponse, isResponsibleError, ResponsibleError } from '../../util/error';

export const getUserHandler: APIGatewayProxyHandler = async (event) => {
const token = (event.headers.Authorization ?? '').replace('Bearer ', '');
Expand All @@ -17,8 +17,8 @@ export const getUserHandler: APIGatewayProxyHandler = async (event) => {
result
});
} catch (e) {
if (e instanceof ResponsibleError) {
return e.response();
if (isResponsibleError(e)) {
return errorResponse(e as ResponsibleError);
}
if (e instanceof JsonWebTokenError || e instanceof TokenExpiredError) {
return createResponse(401, {
Expand Down
6 changes: 3 additions & 3 deletions packages/server/src/user/handler/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { JWT_SECRET } from '../../env';
import { createResponse } from '../../common';
import { assertAccessible } from '../../auth/data';
import { queryUser } from '../data';
import { ResponsibleError } from '../../util/error';
import { errorResponse, isResponsibleError, ResponsibleError } from '../../util/error';

export const queryUserHandler: APIGatewayProxyHandler = async (event) => {
const startsWith = event.queryStringParameters?.starts ?? '';
Expand All @@ -20,8 +20,8 @@ export const queryUserHandler: APIGatewayProxyHandler = async (event) => {
result
});
} catch (e) {
if (e instanceof ResponsibleError) {
return e.response();
if (isResponsibleError(e)) {
return errorResponse(e as ResponsibleError);
}
console.error(e);
return createResponse(500, {
Expand Down
6 changes: 3 additions & 3 deletions packages/server/src/user/handler/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { createResponse } from '../../common';
import type { JwtPayload } from 'jsonwebtoken';
import * as jwt from 'jsonwebtoken';
import { JWT_SECRET } from '../../env';
import { ResponsibleError } from '../../util/error';
import { updateUser } from '../data';
import { assertAccessible } from '../../auth/data';
import { errorResponse, isResponsibleError, ResponsibleError } from '../../util/error';

export const updateUserHandler: APIGatewayProxyHandler = async (event) => {
const token = (event.headers.Authorization ?? '').replace('Bearer ', '');
Expand Down Expand Up @@ -43,8 +43,8 @@ export const updateUserHandler: APIGatewayProxyHandler = async (event) => {
await updateUser(data);
return createResponse(200, { success: true });
} catch (e) {
if (e instanceof ResponsibleError) {
return e.response();
if (isResponsibleError(e)) {
return errorResponse(e as ResponsibleError);
}
console.error(e);
const res = {
Expand Down
60 changes: 29 additions & 31 deletions packages/server/src/util/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,50 @@ import type { APIGatewayProxyResult } from 'aws-lambda';
import { createResponse } from '../common';

export class ResponsibleError extends Error {
message: string;

errorCode: number;

additionalInfo: Record<string, unknown>;

constructor(message?: string, additionalInfo?: Record<string, unknown>) {
errorType: string;

constructor(errorCode: number, errorType: string, message?: string, additionalInfo?: Record<string, unknown>) {
super(message);
this.errorCode = errorCode;
this.errorType = errorType;
this.message = message;
this.additionalInfo = additionalInfo;
}

response(): APIGatewayProxyResult {
return createResponse(500, {
success: false,
error: 500,
error_description: 'Internal error',
...this.additionalInfo
});
}
}

export class UnauthorizedError extends ResponsibleError {
response(): APIGatewayProxyResult {
return createResponse(401, {
success: false,
error: 401,
error_description: this.message,
...this.additionalInfo
});
constructor(message?: string, additionalInfo?: Record<string, unknown>) {
super(401, 'UnauthorizedError', message, additionalInfo);
}
}

export class NotFoundError extends ResponsibleError {
response(): APIGatewayProxyResult {
return createResponse(404, {
success: false,
error: 404,
error_description: this.message,
...this.additionalInfo
});
constructor(message?: string, additionalInfo?: Record<string, unknown>) {
super(404, 'NotFoundError', message, additionalInfo);
}
}

export class CantClaimError extends ResponsibleError {
response(): APIGatewayProxyResult {
return createResponse(403, {
success: false,
error: 403,
error_description: this.message,
...this.additionalInfo
});
constructor(message?: string, additionalInfo?: Record<string, unknown>) {
super(403, 'CantClaimError', message, additionalInfo);
}
}

export function isResponsibleError(error: unknown) {
return typeof error === 'object' && Object.keys(error).includes('errorType');
}

export function errorResponse(error: ResponsibleError, overrideDescription?: string): APIGatewayProxyResult {
return createResponse(error.errorCode, {
success: false,
error: error.errorCode,
error_description: overrideDescription ?? error.message,
...error.additionalInfo
});
}
Loading

0 comments on commit 643c101

Please sign in to comment.