-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7ac9fa3
commit 99be4ab
Showing
9 changed files
with
105 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
samples/web-csr/dressca-frontend/admin/src/shared/error-handler/custom-error-handler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import type { App } from 'vue'; | ||
import { showToast } from '@/services/notification/notificationService'; | ||
import { useRoutingStore } from '@/stores/routing/routing'; | ||
import { router } from '@/router'; | ||
import { customErrorHandlerKey } from '@/shared/injection-symbols'; | ||
import { | ||
CustomErrorBase, | ||
UnauthorizedError, | ||
NetworkError, | ||
ServerError, | ||
} from './custom-error'; | ||
|
||
export interface CustomErrorHandler { | ||
install(app: App): void; | ||
handle( | ||
error: unknown, | ||
callback: () => void, | ||
handlingUnauthorizedError?: (() => void) | null, | ||
handlingNetworkError?: (() => void) | null, | ||
handlingServerError?: (() => void) | null, | ||
): void; | ||
} | ||
|
||
export function createCustomErrorHandler(): CustomErrorHandler { | ||
const customErrorHandler: CustomErrorHandler = { | ||
install: (app: App) => { | ||
app.provide(customErrorHandlerKey, customErrorHandler); | ||
}, | ||
handle: ( | ||
error: unknown, | ||
callback: () => void, | ||
handlingUnauthorizedError: (() => void) | null = null, | ||
handlingNetworkError: (() => void) | null = null, | ||
handlingServerError: (() => void) | null = null, | ||
) => { | ||
// ハンドリングできるエラーの場合はコールバックを実行 | ||
if (error instanceof CustomErrorBase) { | ||
callback(); | ||
|
||
// エラーの種類によって共通処理を行う | ||
// switch だと instanceof での判定ができないため if 文で判定 | ||
if (error instanceof UnauthorizedError) { | ||
if (handlingUnauthorizedError) { | ||
handlingUnauthorizedError(); | ||
} else { | ||
const routingStore = useRoutingStore(); | ||
routingStore.setRedirectFrom( | ||
router.currentRoute.value.path.slice(1), | ||
); | ||
router.push({ name: 'authentication/login' }); | ||
showToast('ログインしてください。'); | ||
} | ||
} else if (error instanceof NetworkError) { | ||
if (handlingNetworkError) { | ||
handlingNetworkError(); | ||
} else { | ||
showToast('ネットワークエラーが発生しました。'); | ||
} | ||
} else if (error instanceof ServerError) { | ||
if (handlingServerError) { | ||
handlingServerError(); | ||
} else { | ||
showToast('サーバーエラーが発生しました。'); | ||
} | ||
} | ||
} else { | ||
// ハンドリングできないエラーの場合は上位にエラーを投げる | ||
throw error; | ||
} | ||
}, | ||
}; | ||
return customErrorHandler; | ||
} |
51 changes: 0 additions & 51 deletions
51
samples/web-csr/dressca-frontend/admin/src/shared/error-handler/error-handler.ts
This file was deleted.
Oops, something went wrong.
7 changes: 7 additions & 0 deletions
7
samples/web-csr/dressca-frontend/admin/src/shared/error-handler/use-custom-error-handler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { inject } from 'vue'; | ||
import type { CustomErrorHandler } from './custom-error-handler'; | ||
import { customErrorHandlerKey } from '../injection-symbols'; | ||
|
||
export function useCustomErrorHandler(): CustomErrorHandler { | ||
return inject(customErrorHandlerKey)!; | ||
} |
6 changes: 6 additions & 0 deletions
6
samples/web-csr/dressca-frontend/admin/src/shared/injection-symbols.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import type { InjectionKey } from 'vue'; | ||
import type { CustomErrorHandler } from './error-handler/custom-error-handler'; | ||
|
||
export const customErrorHandlerKey = Symbol( | ||
'customErrorHandler', | ||
) as InjectionKey<CustomErrorHandler>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters