Error response enhancements, CoreID auth client backend

This commit is contained in:
2022-03-29 01:14:46 -05:00
parent a039b1ff25
commit 8f08b94f74
31 changed files with 736 additions and 52 deletions

View File

@@ -1,4 +1,5 @@
import {Awaitable} from '../support/types'
import {Safe} from '../support/Safe'
/**
* Abstract interface class for a cached object.
@@ -11,6 +12,14 @@ export abstract class Cache {
*/
public abstract fetch(key: string): Awaitable<string|undefined>;
/**
* Fetch a value from the cache by its key as a Safe value.
* @param key
*/
public async safe(key: string): Promise<Safe> {
return new Safe(await this.fetch(key))
}
/**
* Store the given value in the cache by key.
* @param {string} key

View File

@@ -27,3 +27,25 @@ export class ErrorWithContext extends Error {
}
}
}
export function withErrorContext<T>(closure: () => T, context: {[key: string]: any}): T {
try {
return closure()
} catch (e: unknown) {
if ( e instanceof ErrorWithContext ) {
e.context = {
...e.context,
...context,
}
throw e
} else if ( e instanceof Error ) {
const ewc = new ErrorWithContext(e.message, context)
ewc.stack = e.stack
ewc.name = e.name
ewc.originalError = e
throw ewc
} else {
throw e
}
}
}

View File

@@ -1,5 +1,7 @@
import {Integer, isInteger} from './types'
import {ErrorWithContext} from '../error/ErrorWithContext'
import {JSONState} from './Rehydratable'
import {isJSON} from './data'
export class Safe {
protected thrower: (message: string, value: unknown) => never
@@ -22,10 +24,10 @@ export class Safe {
present(): this {
if ( !this.value && this.value !== 0 && this.value !== false ) {
return this
this.thrower('Missing value', this.value)
}
this.thrower('Missing value', this.value)
return this
}
integer(): Integer {
@@ -51,6 +53,23 @@ export class Safe {
return String(this.value)
}
json(): JSONState {
const str = this.string()
if ( !isJSON(str) ) {
this.thrower('Invalid JSON', str)
}
return JSON.parse(str)
}
or(other: unknown): Safe {
if ( !this.value && this.value !== 0 && this.value !== false ) {
return new Safe(other)
}
return this
}
in<T>(allowed: T[]): T {
if ( allowed.includes(this.value as any) ) {
return this.value as T