## 概要 [Task2801: 画面修正(ログイン画面)](https://paruru.nds-tyo.co.jp:8443/tfs/ReciproCollection/fa4924a4-d079-4fab-9fb5-a9a11eb205f0/_workitems/edit/2801) - 以下の修正を実施しました - ログイン画面について、未同意バージョンがある場合、利用規約同意画面に遷移する処理を実装 - 利用規約同意画面(ADB2C以外の画面)からログイン画面に遷移した際も処理継続できるよう対応を実施 - このPull Requestでの対象/対象外 - AcceptToUsePageについては、遷移確認用のダミーページなので対象外でお願いします。 - 影響範囲(他の機能にも影響があるか) - ありません。 ## レビューポイント - 特にレビューしてほしい箇所 1. 既存のLoginPageを以下のように分割しています。 実装内容のイメージあっているか確認お願いします。 - LoginPage→AADB2Cからのリダイレクトを元にLocalStorageアクセス用のキーを生成 - TokenSettingPage→LocalStorageアクセス用のキーを使用してidTokenを取得し各種token生成を実施 1. TokenSettingPage/index.tsxにて、型ガード(isErrorObject)を作成し使用しています。 使い方やガードの実装が妥当か確認お願いします。 ## UIの変更 - 無し ## 動作確認状況 - ローカルで確認を実施 ## 補足 - 相談、参考資料などがあれば
102 lines
2.5 KiB
TypeScript
102 lines
2.5 KiB
TypeScript
import { AxiosError } from "axios";
|
|
import { isError } from "lodash";
|
|
import { ErrorResponse } from "../../api";
|
|
import { errorCodes } from "./code";
|
|
import { ErrorCodeType, ErrorObject } from "./types";
|
|
|
|
export const createErrorObject = (error: unknown): ErrorObject => {
|
|
// 最低限通常のエラーかを判定
|
|
// Error以外のものがthrowされた場合
|
|
// 基本的にないはずだがプログラム上あるので拾う
|
|
if (!isError(error)) {
|
|
return {
|
|
message: "not error type.",
|
|
code: "E009999",
|
|
};
|
|
}
|
|
|
|
// Axiosエラー 通信してのエラーであるかを判定
|
|
if (!isAxiosError(error)) {
|
|
return {
|
|
message: "not axios error.",
|
|
code: "E009999",
|
|
};
|
|
}
|
|
|
|
const errorResponse = error.response;
|
|
if (!errorResponse) {
|
|
return {
|
|
message: error.message,
|
|
code: "E009999",
|
|
statusCode: errorResponse,
|
|
};
|
|
}
|
|
|
|
const { data } = errorResponse;
|
|
|
|
// 想定しているエラーレスポンスの型か判定
|
|
if (!isErrorResponse(data)) {
|
|
return {
|
|
message: error.message,
|
|
code: "E009999",
|
|
statusCode: errorResponse.status,
|
|
};
|
|
}
|
|
|
|
const { message, code } = data;
|
|
|
|
// 想定しているエラーコードかを判定
|
|
if (!isErrorCode(code)) {
|
|
return {
|
|
message,
|
|
code: "E009999",
|
|
statusCode: errorResponse.status,
|
|
};
|
|
}
|
|
|
|
return {
|
|
message,
|
|
code,
|
|
statusCode: errorResponse.status,
|
|
};
|
|
};
|
|
|
|
const isAxiosError = (e: unknown): e is AxiosError => {
|
|
const error = e as AxiosError;
|
|
return error?.isAxiosError ?? false;
|
|
};
|
|
|
|
const isErrorResponse = (error: unknown): error is ErrorResponse => {
|
|
const errorResponse = error as ErrorResponse;
|
|
if (
|
|
errorResponse === undefined ||
|
|
errorResponse.message === undefined ||
|
|
errorResponse.code === undefined
|
|
) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
};
|
|
|
|
const isErrorCode = (errorCode: string): errorCode is ErrorCodeType =>
|
|
errorCodes.includes(errorCode as ErrorCodeType);
|
|
|
|
export const isErrorObject = (
|
|
data: unknown
|
|
): data is { error: ErrorObject } => {
|
|
if (
|
|
data &&
|
|
typeof data === "object" &&
|
|
"error" in data &&
|
|
typeof (data as { error: ErrorObject }).error === "object" &&
|
|
typeof (data as { error: ErrorObject }).error.message === "string" &&
|
|
typeof (data as { error: ErrorObject }).error.code === "string" &&
|
|
(typeof (data as { error: ErrorObject }).error.statusCode === "number" ||
|
|
(data as { error: ErrorObject }).error.statusCode === undefined)
|
|
) {
|
|
return true;
|
|
}
|
|
return false;
|
|
};
|