| 1 | import { describe, it } from "@effect/vitest" |
| 2 | import { strictEqual } from "@effect/vitest/utils" |
| 3 | import { Effect, Schema } from "effect" |
| 4 | import { HttpServerRespondable, HttpServerResponse } from "effect/http" |
| 5 | |
| 6 | describe("HttpServerRespondable", () => { |
| 7 | it.effect("maps SchemaError to a 400 response", () => |
| 8 | Effect.gen(function*() { |
| 9 | const error = yield* Effect.flip(Schema.decodeUnknownEffect(Schema.String)(123)) |
| 10 | strictEqual(Schema.isSchemaError(error), true) |
| 11 | const fallback = HttpServerResponse.empty({ status: 500 }) |
| 12 | const response = yield* HttpServerRespondable.toResponseOrElse(error, fallback) |
| 13 | strictEqual(response.status, 400) |
| 14 | })) |
| 15 | |
| 16 | it.effect("falls back for other errors", () => |
| 17 | Effect.gen(function*() { |
| 18 | const fallback = HttpServerResponse.empty({ status: 500 }) |
| 19 | const response = yield* HttpServerRespondable.toResponseOrElse(new Error("boom"), fallback) |
| 20 | strictEqual(response, fallback) |
| 21 | })) |
| 22 | }) |