-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
66 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,23 @@ | ||
import { ErrorWithRemediation, Remediation } from '@/lib/error' | ||
import { createErrorWithRemediation, Remediation } from '@/lib/error' | ||
|
||
export const present = <T>(t: T | undefined | null, r: Remediation): T => { | ||
export const present = <T>(t: T | undefined | null, r: Remediation, cause?: string): T => { | ||
if (t === undefined) { | ||
throw new ErrorWithRemediation(`expected to be present but was undefined: ${typeof t}.`, r) | ||
throw createErrorWithRemediation({ | ||
name: 'present error', | ||
message: 'expected to be present but was undefined', | ||
cause | ||
}, r) | ||
} | ||
if (t === null) { | ||
throw new ErrorWithRemediation(`expected to be present but was null: ${typeof t}.`, r) | ||
throw createErrorWithRemediation({ | ||
name: 'present error', | ||
message: 'expected to be present but was null', | ||
cause | ||
}, r) | ||
} | ||
return t | ||
} | ||
|
||
export const presentOrSuggestReload = <T>(t: T | undefined | null): T => present(t, Remediation.Reload) | ||
export const presentOrFileBug = <T>(t: T | undefined | null): T => present(t, Remediation.FileBug) | ||
export const presentOrCheckURL = <T>(t: T | undefined | null): T => present(t, Remediation.CheckUrl) | ||
export const presentOrSuggestReload = <T>(t: T | undefined | null, cause?: string): T => present(t, Remediation.Reload, cause) | ||
export const presentOrFileBug = <T>(t: T | undefined | null, cause?: string): T => present(t, Remediation.FileBug, cause) | ||
export const presentOrCheckURL = <T>(t: T | undefined | null, cause?: string): T => present(t, Remediation.CheckUrl, cause) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,32 @@ | ||
import { type NuxtError } from 'nuxt/app' | ||
|
||
export enum Remediation { | ||
None = 'none', | ||
Reload = 'reload', | ||
FileBug = 'file-bug', | ||
CheckUrl = 'check-url', | ||
} | ||
|
||
export class ErrorWithRemediation extends Error { | ||
constructor (msg: string, public readonly remediation: Remediation) { | ||
super(msg) | ||
interface RemediationData { | ||
type: 'remediation' | ||
remediation: Remediation | ||
} | ||
|
||
type PACTAErrorData = RemediationData | ||
|
||
interface PACTAError extends NuxtError { | ||
data: PACTAErrorData | ||
} | ||
|
||
export const createErrorWithRemediation = (err: string | Partial<NuxtError>, r: Remediation): PACTAError => { | ||
const nuxtErr = createError(err) | ||
if (!nuxtErr.data || typeof (nuxtErr.data) !== 'object') { | ||
nuxtErr.data = {} | ||
} | ||
nuxtErr.data.type = 'remediation' | ||
nuxtErr.data.remediation = r | ||
|
||
// TypeScript doesn't automatically pick up that `data` is always set, so we | ||
// give it a nudge. | ||
return nuxtErr as PACTAError | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters