Skip to content

Commit

Permalink
replace /platform RefailError with use of the "cause" property (#3260)
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart authored Jul 16, 2024
1 parent ada68b3 commit 53c0db0
Show file tree
Hide file tree
Showing 42 changed files with 320 additions and 313 deletions.
20 changes: 20 additions & 0 deletions .changeset/dry-trains-tease.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
"@effect/sql-sqlite-react-native": minor
"@effect/platform-node-shared": minor
"@effect/platform-browser": minor
"@effect/sql-sqlite-node": minor
"@effect/sql-sqlite-wasm": minor
"@effect/sql-sqlite-bun": minor
"@effect/platform-node": minor
"@effect/experimental": minor
"@effect/platform-bun": minor
"@effect/sql-drizzle": minor
"@effect/sql-mysql2": minor
"@effect/sql-mssql": minor
"@effect/platform": minor
"@effect/sql-d1": minor
"@effect/sql-pg": minor
"@effect/sql": minor
---

replace /platform RefailError with use of the "cause" property
13 changes: 3 additions & 10 deletions packages/experimental/src/Machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,24 +137,17 @@ export type ActorTypeId = typeof ActorTypeId
* @category errors
*/
export class MachineDefect extends Schema.TaggedError<MachineDefect>()("MachineDefect", {
cause: Schema.Cause({ error: Schema.Never })
cause: Schema.CauseDefectUnknown
}) {
/**
* @since 1.0.0
*/
static wrap<A, E, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<A, MachineDefect, R> {
return Effect.catchAllCause(
Effect.orDie(effect),
(cause) => Effect.fail(new MachineDefect({ cause }))
(cause) => Effect.fail(new MachineDefect({ cause: Cause.squash(cause) }))
)
}

/**
* @since 1.0.0
*/
get message() {
return Cause.pretty(this.cause)
}
}

/**
Expand Down Expand Up @@ -803,7 +796,7 @@ export const boot = <
})
)
),
Effect.catchAllDefect((defect) => Effect.fail(new MachineDefect({ cause: Cause.die(defect) })))
Effect.catchAllDefect((cause) => Effect.fail(new MachineDefect({ cause })))
)
}).pipe(Effect.scoped) as Effect.Effect<
never,
Expand Down
12 changes: 8 additions & 4 deletions packages/experimental/src/MsgPack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ import { Packr, Unpackr } from "msgpackr"
*/
export class MsgPackError extends Data.TaggedError("MsgPackError")<{
readonly reason: "Pack" | "Unpack"
readonly error: unknown
}> {}
readonly cause: unknown
}> {
get message() {
return this.reason
}
}

/**
* @since 1.0.0
Expand All @@ -42,7 +46,7 @@ export const pack = <IE = never, Done = unknown>(): Channel.Channel<
Channel.flatMap(
Effect.try({
try: () => Chunk.of(packr.pack(Chunk.toReadonlyArray(input))),
catch: (error) => new MsgPackError({ reason: "Pack", error })
catch: (cause) => new MsgPackError({ reason: "Pack", cause })
}),
Channel.write
),
Expand Down Expand Up @@ -122,7 +126,7 @@ export const unpack = <IE = never, Done = unknown>(): Channel.Channel<
throw error
}
}),
catch: (error) => new MsgPackError({ reason: "Unpack", error })
catch: (cause) => new MsgPackError({ reason: "Unpack", cause })
})

const loop: Channel.Channel<Chunk.Chunk<unknown>, Chunk.Chunk<Uint8Array>, IE | MsgPackError, IE, Done, Done> =
Expand Down
15 changes: 10 additions & 5 deletions packages/experimental/src/Ndjson.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @since 1.0.0
*/
import { RefailError } from "@effect/platform/Error"
import { TypeIdError } from "@effect/platform/Error"
import type { ParseError } from "@effect/schema/ParseResult"
import * as Schema from "@effect/schema/Schema"
import type * as Cause from "effect/Cause"
Expand All @@ -28,9 +28,14 @@ const encoder = new TextEncoder()
* @since 1.0.0
* @category errors
*/
export class NdjsonError extends RefailError(NdjsonErrorTypeId, "NdjsonError")<{
export class NdjsonError extends TypeIdError(NdjsonErrorTypeId, "NdjsonError")<{
readonly reason: "Pack" | "Unpack"
}> {}
readonly cause: unknown
}> {
get message() {
return this.reason
}
}

/**
* @since 1.0.0
Expand Down Expand Up @@ -62,7 +67,7 @@ export const pack = <IE = never, Done = unknown>(): Channel.Channel<
Chunk.toReadonlyArray(input).map((_) => JSON.stringify(_)).join("\n") + "\n"
)
),
catch: (error) => new NdjsonError({ reason: "Pack", error })
catch: (cause) => new NdjsonError({ reason: "Pack", cause })
}),
Channel.write
),
Expand Down Expand Up @@ -131,7 +136,7 @@ export const unpack = <IE = never, Done = unknown>(): Channel.Channel<
return Channel.mapOutEffect(Channel.pipeTo(loop, Channel.splitLines()), (chunk) =>
Effect.try({
try: () => Chunk.map(chunk, (_) => JSON.parse(_)),
catch: (error) => new NdjsonError({ reason: "Unpack", error })
catch: (cause) => new NdjsonError({ reason: "Unpack", cause })
}))
})

Expand Down
11 changes: 6 additions & 5 deletions packages/experimental/src/Persistence.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @since 1.0.0
*/
import { RefailError, TypeIdError } from "@effect/platform/Error"
import { TypeIdError } from "@effect/platform/Error"
import * as KeyValueStore from "@effect/platform/KeyValueStore"
import type * as ParseResult from "@effect/schema/ParseResult"
import * as Serializable from "@effect/schema/Serializable"
Expand Down Expand Up @@ -60,19 +60,20 @@ export class PersistenceParseError extends TypeIdError(ErrorTypeId, "Persistence
* @since 1.0.0
* @category errors
*/
export class PersistenceBackingError extends RefailError(ErrorTypeId, "PersistenceError")<{
export class PersistenceBackingError extends TypeIdError(ErrorTypeId, "PersistenceError")<{
readonly reason: "BackingError"
readonly method: string
readonly cause: unknown
}> {
/**
* @since 1.0.0
*/
static make(method: string, error: unknown) {
return new PersistenceBackingError({ reason: "BackingError", method, error })
static make(method: string, cause: unknown) {
return new PersistenceBackingError({ reason: "BackingError", method, cause })
}

get message() {
return `${this.method}: ${super.message}`
return this.reason
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/experimental/test/Machine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ describe("Machine", () => {
assert.strictEqual(yield* _(booted.send(new FailBackground())), undefined)
const cause = yield* _(booted.join, Effect.sandbox, Effect.flip)
const failure = Cause.failures(cause).pipe(Chunk.unsafeHead)
assert.deepStrictEqual(failure.cause, Cause.die("error"))
assert.deepStrictEqual(failure.cause, "error")
}).pipe(Effect.scoped, Machine.withTracingEnabled(true), Effect.provide(DevTools.layer()), Effect.runPromise))

test("init context", () =>
Expand Down
21 changes: 13 additions & 8 deletions packages/platform-browser/src/Geolocation.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @since 1.0.0
*/
import { RefailError } from "@effect/platform/Error"
import { TypeIdError } from "@effect/platform/Error"
import * as Context from "effect/Context"
import * as Effect from "effect/Effect"
import * as Either from "effect/Either"
Expand Down Expand Up @@ -64,9 +64,14 @@ export type ErrorTypeId = typeof ErrorTypeId
* @since 1.0.0
* @category errors
*/
export class GeolocationError extends RefailError(ErrorTypeId, "GeolocationError")<{
export class GeolocationError extends TypeIdError(ErrorTypeId, "GeolocationError")<{
readonly reason: "PositionUnavailable" | "PermissionDenied" | "Timeout"
}> {}
readonly cause: unknown
}> {
get message() {
return this.reason
}
}

const makeQueue = (
options:
Expand All @@ -81,11 +86,11 @@ const makeQueue = (
Effect.sync(() =>
navigator.geolocation.watchPosition(
(position) => queue.unsafeOffer(Either.right(position)),
(error) => {
if (error.code === error.PERMISSION_DENIED) {
queue.unsafeOffer(Either.left(new GeolocationError({ reason: "PermissionDenied", error })))
} else if (error.code === error.TIMEOUT) {
queue.unsafeOffer(Either.left(new GeolocationError({ reason: "Timeout", error })))
(cause) => {
if (cause.code === cause.PERMISSION_DENIED) {
queue.unsafeOffer(Either.left(new GeolocationError({ reason: "PermissionDenied", cause })))
} else if (cause.code === cause.TIMEOUT) {
queue.unsafeOffer(Either.left(new GeolocationError({ reason: "Timeout", cause })))
}
},
options
Expand Down
10 changes: 5 additions & 5 deletions packages/platform-browser/src/internal/httpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export const makeXMLHttpRequest = Client.makeDefault((request, url, signal, fibe
new Error.RequestError({
request,
reason: "Transport",
error: xhr.statusText
cause: xhr.statusText
})
))
}
Expand Down Expand Up @@ -98,12 +98,12 @@ const sendBody = (
return next
}),
{
onFailure: (error) =>
onFailure: (cause) =>
Effect.fail(
new Error.RequestError({
request,
reason: "Encode",
error
cause
})
),
onSuccess: (body) => Effect.sync(() => xhr.send(body))
Expand Down Expand Up @@ -286,12 +286,12 @@ class ClientResponseImpl extends IncomingMessageImpl<Error.ResponseError> implem
readonly request: ClientRequest.HttpClientRequest,
source: XMLHttpRequest
) {
super(source, (_) =>
super(source, (cause) =>
new Error.ResponseError({
request,
response: this,
reason: "Decode",
error: _
cause
}))
this[ClientResponse.TypeId] = ClientResponse.TypeId
}
Expand Down
2 changes: 1 addition & 1 deletion packages/platform-browser/src/internal/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const platformWorkerImpl = Worker.makePlatform<globalThis.SharedWorker | globalT
function onError(event: ErrorEvent) {
Deferred.unsafeDone(
deferred,
new WorkerError({ reason: "unknown", error: event.error ?? event.message })
new WorkerError({ reason: "unknown", cause: event.error ?? event.message })
)
}
port.addEventListener("message", onMessage as any)
Expand Down
4 changes: 2 additions & 2 deletions packages/platform-browser/src/internal/workerRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ const platformRunnerImpl = Runner.PlatformRunner.of({
function onMessageError(error: MessageEvent) {
Deferred.unsafeDone(
fiberSet.deferred,
new WorkerError({ reason: "decode", error: error.data })
new WorkerError({ reason: "decode", cause: error.data })
)
}
function onError(error: any) {
Deferred.unsafeDone(
fiberSet.deferred,
new WorkerError({ reason: "unknown", error: error.data })
new WorkerError({ reason: "unknown", cause: error.data })
)
}
function handlePort(port: MessagePort) {
Expand Down
24 changes: 12 additions & 12 deletions packages/platform-bun/src/internal/httpServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,17 +261,17 @@ class ServerRequestImpl extends Inspectable.Class implements ServerRequest.HttpS

get stream(): Stream.Stream<Uint8Array, Error.RequestError> {
return this.source.body
? Stream.fromReadableStream(() => this.source.body as any, (_) =>
? Stream.fromReadableStream(() => this.source.body as any, (cause) =>
new Error.RequestError({
request: this,
reason: "Decode",
error: _
cause
}))
: Stream.fail(
new Error.RequestError({
request: this,
reason: "Decode",
error: "can not create stream from empty body"
description: "can not create stream from empty body"
})
)
}
Expand All @@ -284,11 +284,11 @@ class ServerRequestImpl extends Inspectable.Class implements ServerRequest.HttpS
this.textEffect = Effect.runSync(Effect.cached(
Effect.tryPromise({
try: () => this.source.text(),
catch: (error) =>
catch: (cause) =>
new Error.RequestError({
request: this,
reason: "Decode",
error
cause
})
})
))
Expand All @@ -298,11 +298,11 @@ class ServerRequestImpl extends Inspectable.Class implements ServerRequest.HttpS
get json(): Effect.Effect<unknown, Error.RequestError> {
return Effect.tryMap(this.text, {
try: (_) => JSON.parse(_) as unknown,
catch: (error) =>
catch: (cause) =>
new Error.RequestError({
request: this,
reason: "Decode",
error
cause
})
})
}
Expand All @@ -311,11 +311,11 @@ class ServerRequestImpl extends Inspectable.Class implements ServerRequest.HttpS
return Effect.flatMap(this.text, (_) =>
Effect.try({
try: () => UrlParams.fromInput(new URLSearchParams(_)),
catch: (error) =>
catch: (cause) =>
new Error.RequestError({
request: this,
reason: "Decode",
error
cause
})
}))
}
Expand Down Expand Up @@ -353,11 +353,11 @@ class ServerRequestImpl extends Inspectable.Class implements ServerRequest.HttpS
this.arrayBufferEffect = Effect.runSync(Effect.cached(
Effect.tryPromise({
try: () => this.source.arrayBuffer(),
catch: (error) =>
catch: (cause) =>
new Error.RequestError({
request: this,
reason: "Decode",
error
cause
})
})
))
Expand Down Expand Up @@ -386,7 +386,7 @@ class ServerRequestImpl extends Inspectable.Class implements ServerRequest.HttpS
new Error.RequestError({
request: this,
reason: "Decode",
error: "Not an upgradeable ServerRequest"
description: "Not an upgradeable ServerRequest"
})
))
return
Expand Down
2 changes: 1 addition & 1 deletion packages/platform-bun/src/internal/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const platformWorkerImpl = Worker.makePlatform<globalThis.Worker>()({
function onError(event: ErrorEvent) {
Deferred.unsafeDone(
deferred,
new WorkerError({ reason: "unknown", error: event.error ?? event.message })
new WorkerError({ reason: "unknown", cause: event.error ?? event.message })
)
}
port.addEventListener("message", onMessage)
Expand Down
Loading

0 comments on commit 53c0db0

Please sign in to comment.