Skip to content

Commit e45b6ef

Browse files
authored
refactor(http-recorder): use Schema.TaggedErrorClass for cassette errors (#26729)
1 parent b616543 commit e45b6ef

3 files changed

Lines changed: 18 additions & 14 deletions

File tree

packages/http-recorder/src/cassette.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
import { Context, Data, Effect, FileSystem, Layer } from "effect"
1+
import { Context, Effect, FileSystem, Layer, Schema } from "effect"
22
import * as fs from "node:fs"
33
import * as path from "node:path"
44
import { secretFindings, type SecretFinding } from "./redaction"
55
import { decodeCassette, encodeCassette, type Cassette, type CassetteMetadata, type Interaction } from "./schema"
66

77
const DEFAULT_RECORDINGS_DIR = path.resolve(process.cwd(), "test", "fixtures", "recordings")
88

9-
export class CassetteNotFoundError extends Data.TaggedError("CassetteNotFoundError")<{
10-
readonly cassetteName: string
11-
}> {
9+
export class CassetteNotFoundError extends Schema.TaggedErrorClass<CassetteNotFoundError>()(
10+
"CassetteNotFoundError",
11+
{ cassetteName: Schema.String },
12+
) {
1213
override get message() {
1314
return `Cassette "${this.cassetteName}" not found`
1415
}

packages/http-recorder/src/recorder.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import { Data, Effect, Ref, Scope } from "effect"
1+
import { Effect, Ref, Schema, Scope } from "effect"
22
import type * as CassetteService from "./cassette"
33
import type { CassetteNotFoundError } from "./cassette"
4-
import type { SecretFinding } from "./redaction"
4+
import { SecretFindingSchema } from "./redaction"
55
import type { CassetteMetadata, Interaction } from "./schema"
66

7-
export class UnsafeCassetteError extends Data.TaggedError("UnsafeCassetteError")<{
8-
readonly cassetteName: string
9-
readonly findings: ReadonlyArray<SecretFinding>
10-
}> {
7+
export class UnsafeCassetteError extends Schema.TaggedErrorClass<UnsafeCassetteError>()("UnsafeCassetteError", {
8+
cassetteName: Schema.String,
9+
findings: Schema.Array(SecretFindingSchema),
10+
}) {
1111
override get message() {
1212
return `Refusing to write cassette "${this.cassetteName}" because it contains possible secrets: ${this.findings
1313
.map((finding) => `${finding.path} (${finding.reason})`)

packages/http-recorder/src/redaction.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,13 @@ export const redactHeaders = (
9595
)
9696
}
9797

98-
export type SecretFinding = {
99-
readonly path: string
100-
readonly reason: string
101-
}
98+
import { Schema } from "effect"
99+
100+
export const SecretFindingSchema = Schema.Struct({
101+
path: Schema.String,
102+
reason: Schema.String,
103+
})
104+
export type SecretFinding = Schema.Schema.Type<typeof SecretFindingSchema>
102105

103106
export const secretFindings = (value: unknown): ReadonlyArray<SecretFinding> =>
104107
stringEntries(value).flatMap((entry) => [

0 commit comments

Comments
 (0)