Improve error messages, record error history for use limits

This commit is contained in:
Samuel Elliott
2023-07-05 00:48:14 +01:00
parent 826ee51a68
commit 3e1bd2621a
19 changed files with 446 additions and 176 deletions

View File

@@ -1,3 +1,4 @@
import * as util from 'node:util';
import { AbortError } from 'node-fetch';
import createDebug from './debug.js';
import Loop, { LoopResult } from './loop.js';
@@ -6,6 +7,43 @@ import { ErrorResponse } from '../api/util.js';
const debug = createDebug('nxapi:util:errors');
export class ErrorDescription {
constructor(
readonly type: string,
readonly message: string,
) {}
static getErrorDescription(err: Error | unknown) {
if (err instanceof HasErrorDescription) {
const description = err[ErrorDescriptionSymbol];
if (description) {
return description.message +
(err instanceof Error ? '\n\n--\n\n' + (err.stack ?? err.message) : '');
}
}
if (err instanceof Error) {
return err.stack || err.message;
}
return util.inspect(err, {compact: true});
}
}
export const ErrorDescriptionSymbol = Symbol('ErrorDescription');
export abstract class HasErrorDescription {
abstract get [ErrorDescriptionSymbol](): ErrorDescription | null;
}
Object.defineProperty(HasErrorDescription, Symbol.hasInstance, {
configurable: true,
value: (instance: HasErrorDescription) => {
return instance && ErrorDescriptionSymbol in instance;
},
});
export const temporary_system_errors = {
'ETIMEDOUT': 'request timed out',
'ENOTFOUND': null,