mirror of
https://github.com/elyby/accounts-frontend.git
synced 2024-12-05 04:59:26 +05:30
50 lines
1.3 KiB
TypeScript
50 lines
1.3 KiB
TypeScript
|
import { InternalServerError } from 'services/request';
|
||
|
import { Resp, Middleware } from 'services/request';
|
||
|
import defaultLogger from 'services/logger';
|
||
|
|
||
|
type Logger = typeof defaultLogger;
|
||
|
|
||
|
const ABORT_ERR = 20;
|
||
|
|
||
|
class BsodMiddleware implements Middleware {
|
||
|
dispatchBsod: () => any;
|
||
|
logger: Logger;
|
||
|
|
||
|
constructor(dispatchBsod: () => any, logger: Logger = defaultLogger) {
|
||
|
this.dispatchBsod = dispatchBsod;
|
||
|
this.logger = logger;
|
||
|
}
|
||
|
|
||
|
async catch<T extends Resp<any>>(
|
||
|
resp?: T | InternalServerError | Error,
|
||
|
): Promise<T> {
|
||
|
const { originalResponse }: { originalResponse?: Resp<any> } = (resp ||
|
||
|
{}) as InternalServerError;
|
||
|
|
||
|
if (
|
||
|
resp &&
|
||
|
((resp instanceof InternalServerError &&
|
||
|
(resp.error as any).code !== ABORT_ERR) ||
|
||
|
(originalResponse && /5\d\d/.test(originalResponse.status)))
|
||
|
) {
|
||
|
this.dispatchBsod();
|
||
|
|
||
|
const { message: errorMessage } = resp as { [key: string]: any };
|
||
|
|
||
|
if (!errorMessage || !/NetworkError/.test(errorMessage)) {
|
||
|
let message = 'Unexpected response (BSoD)';
|
||
|
|
||
|
if (errorMessage) {
|
||
|
message = `BSoD: ${errorMessage}`;
|
||
|
}
|
||
|
|
||
|
this.logger.warn(message, { resp });
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return Promise.reject(resp);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export default BsodMiddleware;
|