Files
sam-react-prod/src/lib/api/errors.ts

79 lines
1.8 KiB
TypeScript
Raw Normal View History

/**
* API
*
*
*/
/**
* (401 Unauthorized)
* -
* -
* -
*/
export class AuthError extends Error {
public readonly status = 401;
public readonly code = 'AUTH_ERROR';
constructor(message: string = '인증이 만료되었습니다. 다시 로그인해주세요.') {
super(message);
this.name = 'AuthError';
}
}
/**
* API
* Server Action에서
*/
export interface ApiErrorResponse {
__error: true;
__authError?: boolean;
status: number;
message: string;
code?: string;
}
/**
*
* Server Action에서 401
*/
export function createAuthErrorResponse(message?: string): ApiErrorResponse {
return {
__error: true,
__authError: true,
status: 401,
message: message || '인증이 만료되었습니다. 다시 로그인해주세요.',
code: 'AUTH_ERROR',
};
}
/**
* API
*/
export function createErrorResponse(status: number, message: string, code?: string): ApiErrorResponse {
return {
__error: true,
__authError: status === 401,
status,
message,
code,
};
}
/**
*
*/
export function isApiError(response: unknown): response is ApiErrorResponse {
return (
typeof response === 'object' &&
response !== null &&
'__error' in response &&
(response as ApiErrorResponse).__error === true
);
}
/**
*
*/
export function isAuthError(response: unknown): response is ApiErrorResponse {
return isApiError(response) && response.__authError === true;
}