| ... | @@ -5,6 +5,7 @@ import * as SchemaGetter from "effect/SchemaGetter"; | ... | @@ -5,6 +5,7 @@ import * as SchemaGetter from "effect/SchemaGetter"; |
| 5 | import * as SchemaIssue from "effect/SchemaIssue"; | 5 | import * as SchemaIssue from "effect/SchemaIssue"; |
| 6 | import * as SchemaTransformation from "effect/SchemaTransformation"; | 6 | import * as SchemaTransformation from "effect/SchemaTransformation"; |
| 7 | import { | 7 | import { |
| | 8 | YAMLParseError, |
| 8 | parse as parseYamlString, | 9 | parse as parseYamlString, |
| 9 | stringify as stringifyYamlValue, | 10 | stringify as stringifyYamlValue, |
| 10 | type CreateNodeOptions, | 11 | type CreateNodeOptions, |
| ... | @@ -22,8 +23,14 @@ export type YamlStringifyOptions = DocumentOptions & | ... | @@ -22,8 +23,14 @@ export type YamlStringifyOptions = DocumentOptions & |
| 22 | CreateNodeOptions & | 23 | CreateNodeOptions & |
| 23 | ToStringOptions; | 24 | ToStringOptions; |
| 24 | | 25 | |
| 25 | function formatYamlError(error: unknown): string { | 26 | function formatYamlParseError(error: unknown): string { |
| 26 | return error instanceof Error ? error.message : String(error); | 27 | if (!(error instanceof YAMLParseError)) { |
| | 28 | return "Invalid YAML."; |
| | 29 | } |
| | 30 | |
| | 31 | const position = error.linePos?.[0]; |
| | 32 | const location = position === undefined ? "" : `, line=${position.line}, column=${position.col}`; |
| | 33 | return `Invalid YAML (code=${error.code}${location}).`; |
| 27 | } | 34 | } |
| 28 | | 35 | |
| 29 | /** | 36 | /** |
| ... | @@ -56,7 +63,7 @@ export function parseYaml<E extends string>( | ... | @@ -56,7 +63,7 @@ export function parseYaml<E extends string>( |
| 56 | Effect.try({ | 63 | Effect.try({ |
| 57 | try: () => parseYamlString(input, options) as unknown, | 64 | try: () => parseYamlString(input, options) as unknown, |
| 58 | catch: (error) => | 65 | catch: (error) => |
| 59 | new SchemaIssue.InvalidValue(Option.some(input), { message: formatYamlError(error) }), | 66 | new SchemaIssue.InvalidValue(Option.none(), { message: formatYamlParseError(error) }), |
| 60 | }), | 67 | }), |
| 61 | ); | 68 | ); |
| 62 | } | 69 | } |
| ... | @@ -90,8 +97,8 @@ export function stringifyYaml( | ... | @@ -90,8 +97,8 @@ export function stringifyYaml( |
| 90 | return SchemaGetter.transformOrFail((input: unknown) => | 97 | return SchemaGetter.transformOrFail((input: unknown) => |
| 91 | Effect.try({ | 98 | Effect.try({ |
| 92 | try: () => stringifyYamlValue(input, options), | 99 | try: () => stringifyYamlValue(input, options), |
| 93 | catch: (error) => | 100 | catch: () => |
| 94 | new SchemaIssue.InvalidValue(Option.some(input), { message: formatYamlError(error) }), | 101 | new SchemaIssue.InvalidValue(Option.none(), { message: "Failed to stringify YAML." }), |
| 95 | }), | 102 | }), |
| 96 | ); | 103 | ); |
| 97 | } | 104 | } |