Error response enhancements, CoreID auth client backend
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user