| 1 | import * as Cause from "../../Cause.ts" |
| 2 | import * as SchemaIssue from "../../SchemaIssue.ts" |
| 3 | |
| 4 | /** @internal */ |
| 5 | export function getSchemaIssue(cause: Cause.Cause<SchemaIssue.Issue>): SchemaIssue.Issue | undefined { |
| 6 | let issue: SchemaIssue.Issue | undefined |
| 7 | for (const reason of cause.reasons) { |
| 8 | if (!Cause.isFailReason(reason) || !SchemaIssue.isIssue(reason.error)) { |
| 9 | return undefined |
| 10 | } |
| 11 | issue ??= reason.error |
| 12 | } |
| 13 | return issue |
| 14 | } |
| 15 | |
| 16 | /** @internal */ |
| 17 | export function getSchemaIssueOrThrow( |
| 18 | cause: Cause.Cause<SchemaIssue.Issue>, |
| 19 | message: string |
| 20 | ): SchemaIssue.Issue { |
| 21 | const issue = getSchemaIssue(cause) |
| 22 | if (issue === undefined) { |
| 23 | throw new Error(message, { cause }) |
| 24 | } |
| 25 | return issue |
| 26 | } |