diff --git a/src/cli/Directive.ts b/src/cli/Directive.ts index b51523a..5f9ba2e 100644 --- a/src/cli/Directive.ts +++ b/src/cli/Directive.ts @@ -169,8 +169,11 @@ export abstract class Directive extends AppClass { const optionValues = this.parseOptions(options, argv) this.setOptionValues(optionValues) await this.handle(argv) - } catch (e) { - this.nativeOutput(e.message) + } catch (e: unknown) { + if ( e instanceof Error ) { + this.nativeOutput(e.message) + } + if ( e instanceof OptionValidationError ) { // expecting, value, requirements if ( e.context.expecting ) { @@ -187,6 +190,7 @@ export abstract class Directive extends AppClass { this.nativeOutput(` - ${e.context.value}`) } } + this.nativeOutput('\nUse --help for more info.') } } diff --git a/src/lifecycle/Application.ts b/src/lifecycle/Application.ts index 1f747e4..d6c9294 100644 --- a/src/lifecycle/Application.ts +++ b/src/lifecycle/Application.ts @@ -259,8 +259,12 @@ export class Application extends Container { try { await this.up() await this.down() - } catch (e) { - this.errorHandler(e) + } catch (e: unknown) { + if ( e instanceof Error ) { + this.errorHandler(e) + } + + throw e } } @@ -306,9 +310,14 @@ export class Application extends Container { await unit.up() unit.status = UnitStatus.Started logging.info(`Started ${unit.constructor.name}.`) - } catch (e) { + } catch (e: unknown) { unit.status = UnitStatus.Error - throw this.errorWrapContext(e, {unitName: unit.constructor.name}) + + if ( e instanceof Error ) { + throw this.errorWrapContext(e, {unitName: unit.constructor.name}) + } + + throw e } } @@ -327,7 +336,12 @@ export class Application extends Container { logging.info(`Stopped ${unit.constructor.name}.`) } catch (e) { unit.status = UnitStatus.Error - throw this.errorWrapContext(e, {unitName: unit.constructor.name}) + + if ( e instanceof Error ) { + throw this.errorWrapContext(e, {unitName: unit.constructor.name}) + } + + throw e } } } diff --git a/src/lifecycle/RunLevelErrorHandler.ts b/src/lifecycle/RunLevelErrorHandler.ts index 32f49fa..b03766b 100644 --- a/src/lifecycle/RunLevelErrorHandler.ts +++ b/src/lifecycle/RunLevelErrorHandler.ts @@ -79,10 +79,13 @@ ${contextDisplay} } this.logging.error(errorString, true) - } catch (displayError) { - // The error display encountered an error... - // just throw the original so it makes it out - console.error('RunLevelErrorHandler encountered an error:', displayError.message) // eslint-disable-line no-console + } catch (displayError: unknown) { + if ( displayError instanceof Error ) { + // The error display encountered an error... + // just throw the original so it makes it out + console.error('RunLevelErrorHandler encountered an error:', displayError.message) // eslint-disable-line no-console + } + throw operativeError } } diff --git a/src/orm/connection/PostgresConnection.ts b/src/orm/connection/PostgresConnection.ts index cfce90a..df071c7 100644 --- a/src/orm/connection/PostgresConnection.ts +++ b/src/orm/connection/PostgresConnection.ts @@ -63,10 +63,12 @@ export class PostgresConnection extends Connection { rowCount: result.rowCount, } } catch (e) { - throw this.app().errorWrapContext(e, { - query, - connection: this.name, - }) + if ( e instanceof Error ) { + throw this.app().errorWrapContext(e, { + query, + connection: this.name, + }) + } } } diff --git a/src/service/HTTPServer.ts b/src/service/HTTPServer.ts index e92a40b..8c22d6a 100644 --- a/src/service/HTTPServer.ts +++ b/src/service/HTTPServer.ts @@ -1,5 +1,5 @@ import {Inject, Singleton} from '../di' -import {HTTPStatus, withTimeout} from '../util' +import {ErrorWithContext, HTTPStatus, withTimeout} from '../util' import {Unit} from '../lifecycle/Unit' import {createServer, IncomingMessage, RequestListener, Server, ServerResponse} from 'http' import {Logging} from './Logging' @@ -114,7 +114,11 @@ export class HTTPServer extends Unit { try { await this.kernel.handle(extolloReq) } catch (e) { - await error(e).write(extolloReq) + if ( e instanceof Error ) { + await error(e).write(extolloReq) + } + + await error(new ErrorWithContext('Unknown error occurred.', { e })) } await extolloReq.response.send() diff --git a/src/util/support/BehaviorSubject.ts b/src/util/support/BehaviorSubject.ts index 3280dc5..6b73ec7 100644 --- a/src/util/support/BehaviorSubject.ts +++ b/src/util/support/BehaviorSubject.ts @@ -137,7 +137,7 @@ export class BehaviorSubject { } catch (e) { if ( e instanceof UnsubscribeError ) { this.subscribers = this.subscribers.filter(x => x !== subscriber) - } else if (subscriber.error) { + } else if (subscriber.error && e instanceof Error) { await subscriber.error(e) } else { throw e @@ -181,7 +181,7 @@ export class BehaviorSubject { try { await subscriber.complete(finalValue) } catch (e) { - if ( subscriber.error ) { + if ( subscriber.error && e instanceof Error ) { await subscriber.error(e) } else { throw e diff --git a/src/util/support/path/LocalFilesystem.ts b/src/util/support/path/LocalFilesystem.ts index dc50c41..0ad5b32 100644 --- a/src/util/support/path/LocalFilesystem.ts +++ b/src/util/support/path/LocalFilesystem.ts @@ -92,7 +92,7 @@ export class LocalFilesystem extends Filesystem { isFile: stat.isFile(), } } catch (e) { - if ( e?.code === 'ENOENT' ) { + if ( (e as any)?.code === 'ENOENT' ) { return { path: new UniversalPath(args.storePath, this), exists: false,