Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#259-RefactoringArchitecture: 유틸 함수 변경 #261

Merged
merged 2 commits into from
Jul 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions @types/error.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
interface ErrorResponse {
code: number;
message: string;
}
4 changes: 2 additions & 2 deletions base/hooks/useModal.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useCallback, useState } from 'react';

import { isTrue } from '@Base/utils/check';
import { isNotNil } from '@Base/utils/check';

export interface UseModalProps {
defaultMessage?: string;
Expand All @@ -15,7 +15,7 @@ export function useModal({
const [message, setMessage] = useState(defaultMessage);

const open = useCallback((message?: string) => {
if (isTrue(message)) setMessage(message);
if (isNotNil(message)) setMessage(message);

setIsOpen(true);
}, []);
Expand Down
4 changes: 2 additions & 2 deletions base/hooks/useTrigger.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useCallback, useEffect, useState } from 'react';

import { isFalse } from '@Base/utils/check';
import { isNil } from '@Base/utils/check';

export interface UseTriggerProps {
triggerFn?: () => boolean;
Expand All @@ -19,7 +19,7 @@ export function useTrigger({ triggerFn, onTrigger }: UseTriggerProps) {
}, []);

useEffect(() => {
if (isFalse(triggerFn)) return;
if (isNil(triggerFn)) return;

if (triggerFn()) {
setIsTriggered(true);
Expand Down
44 changes: 22 additions & 22 deletions base/utils/check.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
/**
* checking object if is null + undefined or not
* if object is 0, this functions consider as true
*/
export function isFalse<T>(obj: T | undefined | null): obj is undefined | null {
if (obj === undefined || obj === null) return true;

return false;
}

/**
* checking object if is null + undefined or not
* if object is 0, this functions consider as true
*/
export function isTrue<T>(obj: T | undefined | null): obj is T {
return !isFalse(obj);
}

export function isEmpty<T extends string | Array<unknown>>(obj: T) {
if (obj.length === 0) return true;
return false;
}
/**
* checking object if is null + undefined or not
* if object is 0, this functions consider as true
*/
export function isNil<T>(obj: T | undefined | null): obj is undefined | null {
if (obj === undefined || obj === null) return true;
return false;
}
/**
* checking object if is null + undefined or not
* if object is 0, this functions consider as true
*/
export function isNotNil<T>(obj: T | undefined | null): obj is T {
return !isNil(obj);
}
export function isEmpty<T extends string | Array<unknown>>(obj: T) {
if (obj.length === 0) return true;
return false;
}
11 changes: 11 additions & 0 deletions base/utils/dataExtension.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { isNil } from '@Base/utils/check';

export function getRandomId() {
return Math.random().toString(16).slice(2);
}

export function withDefault<T>(obj: T | null | undefined, defaultValue: T): T {
if (isNil(obj)) return defaultValue;

return obj;
}
3 changes: 3 additions & 0 deletions src/@types/omctError.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
interface OmctErrorResponse extends Pick<ErrorResponse, 'message'> {
code: import('@Constant/errorKeyValue').EOmctErrorNo;
}
33 changes: 24 additions & 9 deletions src/constant/errorKeyValue.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,40 @@
export enum OmctErrorNo {
export enum EOmctErrorNo {
/* 0 ~ 20 : Common Error */
COMMON_ERROR_START = 0,
COMMON_UNEXPECTED_CONDITION = COMMON_ERROR_START,
COMMON_UNEXPECTED_CONDITION,
COMMON_INVALID_PARAMETER,
COMMON_UNKNOWN,
COMMON_NOT_INITIALIZED_CONTEXT,
COMMON_ERROR_FINISH = 20,

/* 21 ~ 30 : Share Error */
SHARE_ERROR_START = COMMON_ERROR_FINISH,
SHARE_CLIPBOARD_COPY_ERROR,
SHARE_ERROR_FINISH = 30,

/* 31 ~ 40 : Firebase Error */
FIREBASE_ERROR_START = SHARE_ERROR_FINISH + 1,
FIREBASE_STORAGE_OBJECT_NOT_FOUND,
FIREBASE_STORAGE_UNIQUE_ID_NOT_FOUND,
FIREBASE_ERROR_END = 40,
}

const omctError = new Map<OmctErrorNo, string>();
const omctError = new Map<EOmctErrorNo, string>();

// prettier-ignore
/* 0 ~ 20 : Common Error */
omctError.set(OmctErrorNo.COMMON_UNEXPECTED_CONDITION, '예기치 못한 에러가 발생하였습니다.');
omctError.set(OmctErrorNo.COMMON_INVALID_PARAMETER, '잘못된 파라미터 입니다.');
{
/* 0 ~ 20 : Common Error */
omctError.set(EOmctErrorNo.COMMON_UNEXPECTED_CONDITION, '예기치 못한 에러가 발생하였습니다.');
omctError.set(EOmctErrorNo.COMMON_INVALID_PARAMETER, '잘못된 파라미터 입니다.');
omctError.set(EOmctErrorNo.COMMON_UNKNOWN, '알 수 없는 에러');
omctError.set(EOmctErrorNo.COMMON_NOT_INITIALIZED_CONTEXT, 'Context가 초기화 되지 않았습니다');

// prettier-ignore
/* 21 ~ 30 : Share Error */
omctError.set(OmctErrorNo.SHARE_CLIPBOARD_COPY_ERROR, '클립보드 복사에 실패했습니다.');
/* 21 ~ 30 : Share Error */
omctError.set(EOmctErrorNo.SHARE_CLIPBOARD_COPY_ERROR, '클립보드 복사에 실패했습니다.');

/* 31 ~ 40: Firebase Error */
omctError.set(EOmctErrorNo.FIREBASE_STORAGE_OBJECT_NOT_FOUND, '이미지를 찾을 수 없습니다.');
omctError.set(EOmctErrorNo.FIREBASE_STORAGE_UNIQUE_ID_NOT_FOUND,'유니크 ID를 만들지 찾지 못했습니다.');
}

export default omctError;
4 changes: 2 additions & 2 deletions src/pages/result/share.subPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faDownload, faLink, faShare } from '@fortawesome/free-solid-svg-icons';
import kakaoIcon from 'public/images/icon/kakaoIcon.png';

import { isEmpty, isFalse } from '@Base/utils/check';
import { isEmpty, isNil } from '@Base/utils/check';
import AlertModal from '@Components/AlertModal';
import { useModal } from '@Base/hooks/useModal';
import ROUTE_PATH from '@Constant/routePath';
Expand Down Expand Up @@ -42,7 +42,7 @@ function ShareSubPage({ resultContainerRef, colorType }: MenuSubPageProps) {
}

const wrapper = resultContainerRef.current;
if (isFalse(wrapper)) return;
if (isNil(wrapper)) return;

const imgName = `${colorType}-result.png`;
captureAndDownload(wrapper, imgName);
Expand Down
8 changes: 3 additions & 5 deletions src/utils/clipboard.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { OmctErrorNo } from '@Constant/errorKeyValue';
import { ShareError } from '@Utils/customError';
import { EOmctErrorNo } from '@Constant/errorKeyValue';
import { OmctError } from '@Utils/omctError';

export async function updateClipboard(newClip: string) {
return navigator.clipboard.writeText(newClip);
Expand All @@ -12,8 +12,6 @@ export async function copyUrl(url: string) {
} catch (error) {
console.error(error);
return 'alertFailCopy';
throw new ShareError({
errorNo: OmctErrorNo.SHARE_CLIPBOARD_COPY_ERROR,
});
throw new OmctError(EOmctErrorNo.SHARE_CLIPBOARD_COPY_ERROR);
}
}
26 changes: 0 additions & 26 deletions src/utils/customError.ts

This file was deleted.

31 changes: 31 additions & 0 deletions src/utils/omctError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { withDefault } from '@Base/utils/dataExtension';
import omctError, { EOmctErrorNo } from '@Constant/errorKeyValue';

import { FirebaseError } from 'firebase/app';

export function parseError(error: unknown): OmctErrorResponse {
let code = EOmctErrorNo.COMMON_UNKNOWN;

if (error instanceof FirebaseError) {
if (error.code.includes('storage/object-not-found')) {
code = EOmctErrorNo.FIREBASE_STORAGE_OBJECT_NOT_FOUND;
}
}

const message = withDefault(
omctError.get(code),
omctError.get(EOmctErrorNo.COMMON_UNKNOWN) as string
);

return { code, message };
}

export class OmctError extends Error {
public code: EOmctErrorNo;

constructor(code: EOmctErrorNo) {
super('OmctError');
this.code = code;
this.message = withDefault(omctError.get(code), '알 수 없는 에러');
}
}