Skip to content

Commit

Permalink
Merge pull request #160 from Nexters/feature/159_error_log
Browse files Browse the repository at this point in the history
에러 로그 추가
  • Loading branch information
yujeongJeon authored Aug 27, 2021
2 parents cb09da6 + 4fab2be commit 5733862
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
24 changes: 22 additions & 2 deletions src/utils/fetcher/AuthInterceptor.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {AxiosResponse} from 'axios'
import {AxiosError, AxiosResponse} from 'axios'
import {RESPONSE} from '$constants'
import {Response} from '$types/response'
import {AccessTokenError, CommonApiError, RedirectArror} from './ApiError'

/**
* @todo 회원 인증 에러 추가
*/
export function AuthInterceptor<T>(res: AxiosResponse<Response<T>>): T {
export function resolveInterceptor<T>(res: AxiosResponse<Response<T>>): T {
const code = res.data.code

switch (code) {
Expand All @@ -20,3 +20,23 @@ export function AuthInterceptor<T>(res: AxiosResponse<Response<T>>): T {
return res.data.result
}
}

class ErrorTable {
statusCode?: number
cookie?: string
requestUrl?: string
constructor(statusCode?: number, cookie?: string, requestUrl?: string) {
this.statusCode = statusCode
this.cookie = cookie
this.requestUrl = requestUrl
}
}

export function rejectInterceptor(error: AxiosError) {
const statusCode = error.response?.status
const {headers, url} = error.config
const errorTable = new ErrorTable(statusCode, headers?.Authorization, url)

console.table(errorTable)
throw error
}
4 changes: 2 additions & 2 deletions src/utils/fetcher/withAxios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {isSSR} from '$utils/env'
import {createMockAxiosInstance} from '$utils/__mocks__/fetcher/createMockAxiosInstance'
import axios, {AxiosInstance} from 'axios'
import {HOST_URL} from 'config'
import {AuthInterceptor} from './AuthInterceptor'
import {rejectInterceptor, resolveInterceptor} from './AuthInterceptor'

export interface MockAxiosInstance {
target: AxiosInstance
Expand Down Expand Up @@ -62,7 +62,7 @@ export const createErrorResponse = <T>(data: T): Response<T> => ({

const createAxiosInstance = () => {
const instance = axios.create()
instance.interceptors.response.use(AuthInterceptor)
instance.interceptors.response.use(resolveInterceptor, rejectInterceptor)
return extendWithMock(instance)
}

Expand Down

0 comments on commit 5733862

Please sign in to comment.