| 11220 | * @since 4.0.0 |
| 11221 | */ |
| 11222 | export function CauseReason<E extends Constraint, D extends Constraint>( |
| 11223 | error: E, |
| 11224 | defect: D |
| 11225 | ): CauseReason<E, D> { |
| 11226 | const schema = declareConstructor< |
| 11227 | Cause_.Reason<E["Type"]>, |
| 11228 | Cause_.Reason<E["Encoded"]>, |
| 11229 | CauseReasonIso<E, D> |
| 11230 | >()( |
| 11231 | [error, defect], |
| 11232 | ([error, defect]) => (input, ast, options) => { |
| 11233 | if (!Cause_.isReason(input)) { |
| 11234 | return Effect.fail(new SchemaIssue.InvalidType(ast, input, options)) |
| 11235 | } |
| 11236 | switch (input._tag) { |
| 11237 | case "Fail": |
| 11238 | return Effect.mapBothEager( |
| 11239 | SchemaParser.decodeUnknownEffect(error)(input.error, options), |
| 11240 | { |
| 11241 | onSuccess: Cause_.makeFailReason, |
| 11242 | onFailure: (issue) => SchemaIssue.makeCompositeAtKey(ast, "error", issue, input, options) |
| 11243 | } |
| 11244 | ) |
| 11245 | case "Die": |
| 11246 | return Effect.mapBothEager( |
| 11247 | SchemaParser.decodeUnknownEffect(defect)(input.defect, options), |
| 11248 | { |
| 11249 | onSuccess: Cause_.makeDieReason, |
| 11250 | onFailure: (issue) => SchemaIssue.makeCompositeAtKey(ast, "defect", issue, input, options) |
| 11251 | } |
| 11252 | ) |
| 11253 | case "Interrupt": |
| 11254 | return Effect.succeed(input) |
| 11255 | } |
| 11256 | }, |
| 11257 | { |
| 11258 | representation: { |
| 11259 | id: "effect/schema/CauseReason", |
| 11260 | payload: null |
| 11261 | }, |
| 11262 | expected: "Cause.Failure", |
| 11263 | toCodec: ([error, defect]) => |
| 11264 | link<Cause_.Reason<E["Encoded"]>>()( |
| 11265 | Union([ |
| 11266 | Struct({ _tag: Literal("Fail"), error }), |
| 11267 | Struct({ _tag: Literal("Die"), defect }), |
| 11268 | Struct({ |
| 11269 | _tag: Literal("Interrupt"), |
| 11270 | fiberId: UndefinedOr(Finite) |
| 11271 | }) |
| 11272 | ]), |
| 11273 | SchemaTransformation.transform({ |
| 11274 | decode: (e) => { |
| 11275 | switch (e._tag) { |
| 11276 | case "Fail": |
| 11277 | return Cause_.makeFailReason(e.error) |
| 11278 | case "Die": |
| 11279 | return Cause_.makeDieReason(e.defect) |