Unify Harbor error handling across server and client
This commit is contained in:
33
src/shared/errors.js
Normal file
33
src/shared/errors.js
Normal file
@@ -0,0 +1,33 @@
|
||||
export const ERROR_DEFINITIONS = Object.freeze({
|
||||
CONTROL_UNREACHABLE: { status: 503, message: 'Harbor сейчас недоступен.', retryable: true },
|
||||
REQUEST_INVALID: { status: 400, message: 'Запрос содержит некорректные данные.', retryable: false },
|
||||
ENDPOINT_NOT_FOUND: { status: 404, message: 'Запрошенный API-метод не найден.', retryable: false },
|
||||
SUBSCRIPTION_INVALID: { status: 400, message: 'Ссылка подписки недействительна.', retryable: false },
|
||||
PROVIDER_UNAVAILABLE: { status: 502, message: 'Провайдер подписки временно недоступен.', retryable: true },
|
||||
STATE_CONFLICT: { status: 409, message: 'Данные изменились во время операции.', retryable: true },
|
||||
SERVER_NOT_FOUND: { status: 404, message: 'Выбранный сервер больше недоступен.', retryable: false },
|
||||
CONFIG_INVALID: { status: 422, message: 'Конфигурация VPN недействительна.', retryable: false },
|
||||
PROCESS_START_FAILED: { status: 503, message: 'Не удалось запустить VPN-процесс.', retryable: true },
|
||||
OPERATION_IN_PROGRESS: { status: 409, message: 'Другая операция ещё выполняется.', retryable: true },
|
||||
UNKNOWN: { status: 500, message: 'Не удалось выполнить действие.', retryable: false },
|
||||
});
|
||||
|
||||
export function errorDefinition(code) {
|
||||
return ERROR_DEFINITIONS[code] || ERROR_DEFINITIONS.UNKNOWN;
|
||||
}
|
||||
|
||||
export class HarborError extends Error {
|
||||
constructor(code, { cause, details } = {}) {
|
||||
const definition = errorDefinition(code);
|
||||
super(definition.message, { cause });
|
||||
this.name = 'HarborError';
|
||||
this.code = ERROR_DEFINITIONS[code] ? code : 'UNKNOWN';
|
||||
this.status = definition.status;
|
||||
this.retryable = definition.retryable;
|
||||
this.details = details;
|
||||
}
|
||||
}
|
||||
|
||||
export function normalizeHarborError(error) {
|
||||
return error instanceof HarborError ? error : new HarborError('UNKNOWN', { cause: error });
|
||||
}
|
||||
Reference in New Issue
Block a user