kokkai
Version:
A TypeScript client for the Kokkai API, providing access to Japanese Diet data.
109 lines (108 loc) • 4.02 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.KokkaiError = void 0;
exports.handleApiError = handleApiError;
const axios_1 = require("axios");
/**
* 国会APIクライアント固有のエラー
*/
class KokkaiError extends Error {
constructor(message, code, details, originalError) {
super(message);
this.name = 'KokkaiError';
this.code = code;
this.details = details;
this.originalError = originalError;
if (Error.captureStackTrace) {
Error.captureStackTrace(this, KokkaiError);
}
Object.setPrototypeOf(this, KokkaiError.prototype);
}
}
exports.KokkaiError = KokkaiError;
/**
* APIからのエラーレスポンスを処理し、KokkaiErrorを生成する
* @param error AxiosエラーオブジェクトまたはAPIエラーレスポンス
* @returns KokkaiErrorインスタンス
*/
function handleApiError(error) {
if (error instanceof KokkaiError) {
return error; // すでに処理済みならそのまま返す
}
let message = 'An unexpected error occurred.';
let code;
let details;
let originalError;
if ((0, axios_1.isAxiosError)(error)) {
originalError = error;
message = `Request failed: ${error.message}`;
code = error.code || error.response?.status;
if (error.response?.data) {
const errorData = error.response.data;
({ message, details } = extractErrorDetails(errorData, message));
}
}
else if (error instanceof Error) {
originalError = error;
message = error.message;
}
return new KokkaiError(message, code, details, originalError);
}
/**
* エラーデータから詳細情報を抽出する
* @param errorData APIからのエラーデータ
* @param defaultMessage デフォルトのエラーメッセージ
* @returns 抽出されたエラーメッセージと詳細
*/
function extractErrorDetails(errorData, defaultMessage) {
if (typeof errorData === 'object' && errorData !== null) {
if ('message' in errorData && typeof errorData.message === 'string') {
defaultMessage = errorData.message;
}
if ('details' in errorData && Array.isArray(errorData.details)) {
return { message: defaultMessage, details: errorData.details.filter((d) => typeof d === 'string') };
}
if (isXmlErrorResponse(errorData)) {
return extractXmlErrorDetails(errorData, defaultMessage);
}
}
return { message: defaultMessage };
}
/**
* XML形式のエラーレスポンスかどうかを判定する
* @param data エラーデータ
* @returns XML形式のエラーレスポンスである場合はtrue
*/
function isXmlErrorResponse(data) {
return (typeof data === 'object' &&
data !== null &&
'data' in data &&
typeof data.data === 'object' &&
'diagnostics' in data.data &&
typeof data.data.diagnostics === 'object' &&
'diagnostic' in data.data.diagnostics &&
typeof data.data.diagnostics.diagnostic === 'object');
}
/**
* XML形式のエラーレスポンスから詳細情報を抽出する
* @param errorData XML形式のエラーデータ
* @param defaultMessage デフォルトのエラーメッセージ
* @returns 抽出されたエラーメッセージと詳細
*/
function extractXmlErrorDetails(errorData, defaultMessage) {
const diagnostic = errorData.data.diagnostics.diagnostic;
let message = defaultMessage;
let details;
if ('message' in diagnostic && typeof diagnostic.message === 'string') {
message = diagnostic.message;
}
if ('details' in diagnostic) {
if (typeof diagnostic.details === 'string') {
details = [diagnostic.details];
}
else if (Array.isArray(diagnostic.details)) {
details = diagnostic.details.filter((d) => typeof d === 'string');
}
}
return { message, details };
}