Skip to content

Commit

Permalink
refactor(gen-error-by-server.ts): ๐Ÿ’กRefactor error handling in API logger
Browse files Browse the repository at this point in the history
  • Loading branch information
AlgoRoots committed Mar 8, 2024
1 parent 785f1f1 commit f6ff19f
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 57 deletions.
9 changes: 5 additions & 4 deletions src/utils/logger/api-logger.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { CSSProperties } from 'react';

import { AxiosRequestConfig } from 'axios';
import { AxiosRequestConfig, isAxiosError } from 'axios';

import { formatServerErrors } from './format-server-errors';
import { genErrorByServer } from './gen-error-by-server';
import styledConsole, { StyledConsoleArgs } from './styled-console';

interface ApiLoggerArgs extends Pick<StyledConsoleArgs, 'method'> {
Expand All @@ -25,8 +25,9 @@ export const apiLogger = ({

const errors = (() => {
if (consoleMethod !== 'error') return '';
const errors = formatServerErrors(resData);
return errors.messages;
if (!isAxiosError(resData)) return '';
const errors = genErrorByServer(resData);
return errors.messagesWithKey;
})();

styledConsole({
Expand Down
53 changes: 0 additions & 53 deletions src/utils/logger/format-server-errors.ts

This file was deleted.

85 changes: 85 additions & 0 deletions src/utils/logger/gen-error-by-server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { AxiosError } from 'axios';

export const defMessage = '์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ณ ๊ฐ์„ผํ„ฐ์— ๋ฌธ์˜ํ•ด์ฃผ์„ธ์š”.';

export type ErrorMessage = {
[key: string]: any;
};

type FormattedError = {
name: string;
message: string;
};

export type GenErrorByServerType = {
defMessage?: string;
list?: FormattedError[];
messagesWithKey?: string;
messages?: string;
};

type ErrorMsgType = Record<string, string[]>;

const isErrorMessageValid = (errorMsg: any): errorMsg is ErrorMsgType => {
if (typeof errorMsg !== 'object' || errorMsg === null) {
return false;
}

return Object.values(errorMsg).every(
(value) =>
Array.isArray(value) && value.every((item) => typeof item === 'string'),
);
};

const formatErrorMessage = (
errorKey: string,
errorMessage: string[],
): string => {
const message = errorMessage.join('\n');
return `[${errorKey}]: ${message}`;
};

/**
* ์„œ๋ฒ„์—์„œ ๋ฐœ์ƒํ•œ ์˜ค๋ฅ˜๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์—๋Ÿฌ ๋ฉ”์„ธ์ง€ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
* ์™ธ๋ถ€ ๋ฐฑ์—”๋“œ์™€ ํ˜‘์—…์‹œ์— ์—๋Ÿฌํƒ€์ž…์„ ํ™•์ธํ•ด์ฃผ์„ธ์š”.
* api logger์—์„œ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์œผ๋ฉฐ, ์—๋Ÿฌ ๋ฉ”์„ธ์ง€๋ฅผ ํ†ตํ•ด ํ† ์ŠคํŠธ, ์–ผ๋Ÿฟ ๋“ฑ์— ์ ์šฉ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
*
* @template T - AxiosError ํƒ€์ž…์˜ ์ œ๋„ค๋ฆญ ๋งค๊ฐœ๋ณ€์ˆ˜์ž…๋‹ˆ๋‹ค.
* @param errors - AxiosError ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค.
* @returns ์„œ๋ฒ„์—์„œ ๋ฐœ์ƒํ•œ ์˜ค๋ฅ˜์— ๊ธฐ๋ฐ˜ํ•œ ์—๋Ÿฌ๋ฉ”์„ธ์ง€ ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค.
*/
export const genErrorByServer = <
T extends AxiosError<{ message: ErrorMessage }, any>,
>(
errors: T,
): GenErrorByServerType => {
const errorMsg = errors.response?.data.message;

if (!errorMsg || !isErrorMessageValid(errorMsg)) {
return { messagesWithKey: errors.message };
}

const formattedErrorsWithKey: FormattedError[] = Object.keys(errorMsg)
.filter((key) => key in errorMsg)
.map((key) => {
const errorKey = key;
const errorMessage = errorMsg[key];
return {
name: errorKey,
message: formatErrorMessage(errorKey, errorMessage),
};
});

const errorMessagesWithKey = formattedErrorsWithKey
.map((error) => error.message)
.join('\n');

const errorMessages = Object.values(errorMsg).join('\n');

return {
defMessage,
list: formattedErrorsWithKey,
messagesWithKey: errorMessagesWithKey,
messages: errorMessages,
};
};

0 comments on commit f6ff19f

Please sign in to comment.