|
|
@@ -5,13 +5,37 @@ interface ErrorLike {
|
|
|
name?: string
|
|
|
_tag?: string
|
|
|
message?: string
|
|
|
- data?: Record<string, any>
|
|
|
+ data?: Record<string, unknown>
|
|
|
+}
|
|
|
+
|
|
|
+type ConfigIssue = { message: string; path: string[] }
|
|
|
+
|
|
|
+function isRecord(input: unknown): input is Record<string, unknown> {
|
|
|
+ return typeof input === "object" && input !== null
|
|
|
}
|
|
|
|
|
|
function isTaggedError(error: unknown, tag: string): boolean {
|
|
|
- return (
|
|
|
- typeof error === "object" && error !== null && "_tag" in error && (error as Record<string, unknown>)._tag === tag
|
|
|
- )
|
|
|
+ return isRecord(error) && error._tag === tag
|
|
|
+}
|
|
|
+
|
|
|
+function configData(input: unknown, tag: string): Record<string, unknown> | undefined {
|
|
|
+ if (!isRecord(input)) return undefined
|
|
|
+ if (input.name === tag && isRecord(input.data)) return input.data
|
|
|
+ if (input._tag === tag) return input
|
|
|
+ return undefined
|
|
|
+}
|
|
|
+
|
|
|
+function stringField(input: Record<string, unknown>, key: string): string | undefined {
|
|
|
+ return typeof input[key] === "string" ? input[key] : undefined
|
|
|
+}
|
|
|
+
|
|
|
+function configIssues(input: Record<string, unknown>): ConfigIssue[] {
|
|
|
+ return Array.isArray(input.issues)
|
|
|
+ ? input.issues.filter((issue): issue is ConfigIssue => {
|
|
|
+ if (!isRecord(issue)) return false
|
|
|
+ return typeof issue.message === "string" && Array.isArray(issue.path) && issue.path.every((x) => typeof x === "string")
|
|
|
+ })
|
|
|
+ : []
|
|
|
}
|
|
|
|
|
|
export function FormatError(input: unknown) {
|
|
|
@@ -35,7 +59,7 @@ export function FormatError(input: unknown) {
|
|
|
// ProviderModelNotFoundError: { providerID: string, modelID: string, suggestions?: string[] }
|
|
|
if (NamedError.hasName(input, "ProviderModelNotFoundError")) {
|
|
|
const data = (input as ErrorLike).data
|
|
|
- const suggestions: string[] = Array.isArray(data?.suggestions) ? data.suggestions : []
|
|
|
+ const suggestions = Array.isArray(data?.suggestions) ? data.suggestions.filter((x) => typeof x === "string") : []
|
|
|
return [
|
|
|
`Model not found: ${data?.providerID}/${data?.modelID}`,
|
|
|
...(suggestions.length ? ["Did you mean: " + suggestions.join(", ")] : []),
|
|
|
@@ -50,28 +74,30 @@ export function FormatError(input: unknown) {
|
|
|
}
|
|
|
|
|
|
// ConfigJsonError: { path: string, message?: string }
|
|
|
- if (NamedError.hasName(input, "ConfigJsonError")) {
|
|
|
- const data = (input as ErrorLike).data
|
|
|
- return `Config file at ${data?.path} is not valid JSON(C)` + (data?.message ? `: ${data.message}` : "")
|
|
|
+ const configJson = configData(input, "ConfigJsonError")
|
|
|
+ if (configJson) {
|
|
|
+ const message = stringField(configJson, "message")
|
|
|
+ return `Config file at ${stringField(configJson, "path")} is not valid JSON(C)` + (message ? `: ${message}` : "")
|
|
|
}
|
|
|
|
|
|
// ConfigDirectoryTypoError: { dir: string, path: string, suggestion: string }
|
|
|
- if (NamedError.hasName(input, "ConfigDirectoryTypoError")) {
|
|
|
- const data = (input as ErrorLike).data
|
|
|
- return `Directory "${data?.dir}" in ${data?.path} is not valid. Rename the directory to "${data?.suggestion}" or remove it. This is a common typo.`
|
|
|
+ const configDirectoryTypo = configData(input, "ConfigDirectoryTypoError")
|
|
|
+ if (configDirectoryTypo) {
|
|
|
+ return `Directory "${stringField(configDirectoryTypo, "dir")}" in ${stringField(configDirectoryTypo, "path")} is not valid. Rename the directory to "${stringField(configDirectoryTypo, "suggestion")}" or remove it. This is a common typo.`
|
|
|
}
|
|
|
|
|
|
// ConfigFrontmatterError: { message: string }
|
|
|
- if (NamedError.hasName(input, "ConfigFrontmatterError")) {
|
|
|
- return (input as ErrorLike).data?.message ?? ""
|
|
|
+ const configFrontmatter = configData(input, "ConfigFrontmatterError")
|
|
|
+ if (configFrontmatter) {
|
|
|
+ return stringField(configFrontmatter, "message") ?? ""
|
|
|
}
|
|
|
|
|
|
// ConfigInvalidError: { path?: string, message?: string, issues?: Array<{ message: string, path: string[] }> }
|
|
|
- if (NamedError.hasName(input, "ConfigInvalidError")) {
|
|
|
- const data = (input as ErrorLike).data
|
|
|
- const path = data?.path
|
|
|
- const message = data?.message
|
|
|
- const issues: Array<{ message: string; path: string[] }> = Array.isArray(data?.issues) ? data.issues : []
|
|
|
+ const configInvalid = configData(input, "ConfigInvalidError")
|
|
|
+ if (configInvalid) {
|
|
|
+ const path = stringField(configInvalid, "path")
|
|
|
+ const message = stringField(configInvalid, "message")
|
|
|
+ const issues = configIssues(configInvalid)
|
|
|
return [
|
|
|
`Configuration is invalid${path && path !== "config" ? ` at ${path}` : ""}` + (message ? `: ${message}` : ""),
|
|
|
...issues.map((issue) => "↳ " + issue.message + " " + issue.path.join(".")),
|