defects.test.ts
This file
Path
packages/telemetry/test/defects.test.ts
217 lines · 6.5 KiB · TypeScript
Last changed by
Merge observability/log-levels into mainoscabrielbefore adoption7b71d37
History of this fileTake it
Merge observability into mainoscabrielbefore adoption1985111
lines 1–60why, one step back1import { describe, expect, it } from "@effect/vitest";2import { ProjectName } from "@repo/protocol/ids";3import { UnknownProject, VerifierBlocked } from "@repo/protocol/refusal";4import { Cause, Effect, Layer, Redacted } from "effect";56import { reportDefects, reportedElsewhere } from "../src/defects.ts";7import type { DefectKeeping } from "../src/defects.ts";8import { recordingConsole } from "../src/logger/memory.ts";9import { recordingTracing } from "../src/spans/memory.ts";10import { Telemetry } from "../src/telemetry.ts";1112// A boundary that hands every failure to the reporters, as effect's HTTP and13// RPC servers do with a request's: telling a defect from a refusal is the14// reporter's job.15const boundary = Effect.withErrorReporting;1617const reported = <A, E>(18 body: Effect.Effect<A, E>,19 secrets: readonly Redacted.Redacted[] = [],20 defects: DefectKeeping = "whole"21) =>22 Effect.gen(function* () {23 const console = recordingConsole();24 const recording = recordingTracing();2526 yield* body.pipe(27 Effect.withSpan("Project.claim"),28 Effect.provide(29 Layer.merge(30 Telemetry({ defects, secrets, tracing: recording.tracing }),31 console.layer32 )33 ),34 Effect.exit35 );3637 return {38 errors: console.errors(),39 spans: recording.spans(),40 written: console.written(),41 };42 });4344const datekit = ProjectName.make("alice/datekit");4546describe("a defect", () => {47 it.effect(48 "is reported once, at Error, with its cause, its stack and the invocation's ids, however many boundaries it crosses",49 () =>50 Effect.gen(function* () {51 const { errors } = yield* reported(52 Effect.die(new Error("the store is gone")).pipe(53 boundary,54 boundary,55 Effect.annotateLogs({ project: datekit, requestId: "push-1" })56 )57 );5859 expect(errors).toHaveLength(1);60 expect(errors[0]).toMatchObject({Merge observability/log-levels into mainoscabrielbefore adoption7b71d37
line 61why, one step back61 level: "error",Merge observability into mainoscabrielbefore adoption1985111
lines 62–154why, one step back62 message: "Error: the store is gone",63 project: datekit,64 requestId: "push-1",65 });66 expect(errors[0]).toHaveProperty(67 "cause",68 expect.stringMatching(/^Error: the store is gone\n\s+at /u)69 );70 })71 );7273 it.effect("is reported without the refusal beside it", () =>74 Effect.gen(function* () {75 const { errors } = yield* reported(76 Effect.failCause(77 Cause.fromReasons([78 Cause.makeFailReason(new VerifierBlocked({})),79 Cause.makeDieReason(new Error("the store is gone")),80 ])81 ).pipe(boundary)82 );8384 expect(errors).toHaveLength(1);85 expect(JSON.stringify(errors)).not.toContain("VerifierBlocked");86 })87 );8889 it.effect(90 "is reported at the innermost boundary, with the ids its lines carry there, and passes every boundary further out",91 () =>92 Effect.gen(function* () {93 const { errors } = yield* reported(94 Effect.forEach(95 [new Error("the store is gone"), "the Project DO has no name"],96 (defect) =>97 Effect.die(defect).pipe(98 reportDefects,99 Effect.annotateLogs({ requestId: "push-1" }),100 reportDefects,101 boundary,102 Effect.exit103 )104 )105 );106107 expect(errors).toHaveLength(2);108 expect(errors).toMatchObject([109 { message: "Error: the store is gone", requestId: "push-1" },110 { message: "Error: the Project DO has no name", requestId: "push-1" },111 ]);112 })113 );114115 it.effect(116 "is not reported again once the side that held its stack reported it",117 () =>118 Effect.gen(function* () {119 const { errors } = yield* reported(120 Effect.failCause(121 reportedElsewhere(122 Cause.fromReasons<never>([123 Cause.makeDieReason(new Error("the store is gone")),124 Cause.makeDieReason("the Project DO has no name"),125 ])126 )127 ).pipe(boundary)128 );129130 expect(errors).toStrictEqual([]);131 })132 );133134 it.effect(135 "is reported by its name and frames alone where its text may quote what no log may hold",136 () =>137 Effect.gen(function* () {138 const repo = "alice--datekit--private";139140 const { errors } = yield* reported(141 Effect.forEach(142 [143 new TypeError(`the layer repo ${repo} has no commit`, {144 cause: new Error(`no tree in ${repo}`),145 }),146 `the layer repo ${repo} lost a blob its tree names`,147 ],148 (defect) => Effect.die(defect).pipe(boundary, Effect.exit)149 ),150 [],151 "name"152 );153154 expect(errors).toMatchObject([Merge observability/log-levels into mainoscabrielbefore adoption7b71d37
lines 155–156why, one step back155 { level: "error", message: "TypeError: <withheld>" },156 { level: "error", message: "Error: <withheld>" },Merge observability into mainoscabrielbefore adoption1985111
lines 157–217why, one step back157 ]);158 expect(errors[0]).toHaveProperty(159 "cause",160 expect.stringMatching(/^TypeError: <withheld>\n\s+at /u)161 );162 expect(errors[1]).toHaveProperty(163 "cause",164 expect.stringMatching(/^Error: <withheld>\n\s+at /u)165 );166 expect(JSON.stringify(errors)).not.toContain(repo);167 })168 );169});170171describe("a refusal", () => {172 it.effect("is never reported as an error", () =>173 Effect.gen(function* () {174 const { errors, written } = yield* reported(175 Effect.all([176 Effect.fail(new VerifierBlocked({})).pipe(boundary, Effect.exit),177 Effect.fail(new UnknownProject({ project: datekit })).pipe(178 boundary,179 Effect.exit180 ),181 ])182 );183184 expect([errors, written]).toStrictEqual([[], []]);185 })186 );187});188189describe("what is reported", () => {190 it.effect("carries no sealed and no plain env value", () =>191 Effect.gen(function* () {192 const sealed = "0f".repeat(32);193 const plain = "sk_live_0123456789abcdef";194195 const { errors, spans } = yield* reported(196 Effect.forEach(197 [198 new Error(`could not unwrap with ${sealed}`),199 { key: "STRIPE_KEY", value: Redacted.make(plain) },200 ],201 (defect) => Effect.die(defect).pipe(boundary, Effect.exit)202 ),203 [Redacted.make(sealed)]204 );205206 const text = JSON.stringify({207 errors,208 exceptions: spans.flatMap(({ exceptions }) => exceptions),209 });210211 expect(errors).toHaveLength(2);212 expect(text).toContain("could not unwrap with <redacted>");213 expect(text).not.toContain(sealed);214 expect(text).not.toContain(plain);215 })216 );217});