From c6925b2295ed6045f20b9405b6ea4e87a1b7c29b Mon Sep 17 00:00:00 2001 From: Tim Date: Thu, 18 Jul 2024 15:16:15 +1200 Subject: [PATCH] fix YieldableError rendering on bun --- .changeset/unlucky-peaches-report.md | 5 +++++ packages/effect/src/internal/core.ts | 26 ++++++++++++++++---------- 2 files changed, 21 insertions(+), 10 deletions(-) create mode 100644 .changeset/unlucky-peaches-report.md diff --git a/.changeset/unlucky-peaches-report.md b/.changeset/unlucky-peaches-report.md new file mode 100644 index 00000000000..026077e9a4c --- /dev/null +++ b/.changeset/unlucky-peaches-report.md @@ -0,0 +1,5 @@ +--- +"effect": patch +--- + +fix YieldableError rendering on bun diff --git a/packages/effect/src/internal/core.ts b/packages/effect/src/internal/core.ts index f33a15c022c..3683d8c6974 100644 --- a/packages/effect/src/internal/core.ts +++ b/packages/effect/src/internal/core.ts @@ -29,7 +29,7 @@ import type * as MetricLabel from "../MetricLabel.js" import * as MutableRef from "../MutableRef.js" import * as Option from "../Option.js" import { pipeArguments } from "../Pipeable.js" -import { hasProperty, isObject, isPromiseLike, isString, type Predicate, type Refinement } from "../Predicate.js" +import { hasProperty, isObject, isPromiseLike, type Predicate, type Refinement } from "../Predicate.js" import type * as Request from "../Request.js" import type * as BlockedRequests from "../RequestBlock.js" import type * as RequestResolver from "../RequestResolver.js" @@ -2174,21 +2174,25 @@ export const causeSquashWith = dual< // Errors // ----------------------------------------------------------------------------- +function customToStringRenderer(this: Cause.YieldableError) { + return this.stack ? `${this.toString()}\n${this.stack.split("\n").slice(1).join("\n")}` : this.toString() +} + /** @internal */ export const YieldableError: new(message?: string, options?: ErrorOptions) => Cause.YieldableError = (function() { class YieldableError extends globalThis.Error { + constructor(message?: string, options?: ErrorOptions) { + super(message, options) + if (this.toString !== globalThis.Error.prototype.toString) { + ;(this as any)[NodeInspectSymbol] = customToStringRenderer + } + } commit() { return fail(this) } toJSON() { return { ...this } } - [NodeInspectSymbol]() { - if (this.toString !== globalThis.Error.prototype.toString) { - return this.stack ? `${this.toString()}\n${this.stack.split("\n").slice(1).join("\n")}` : this.toString() - } - return this - } } Object.assign(YieldableError.prototype, StructuralCommitPrototype) return YieldableError as any @@ -2311,12 +2315,14 @@ export const UnknownExceptionTypeId: Cause.UnknownExceptionTypeId = Symbol.for( ) as Cause.UnknownExceptionTypeId /** @internal */ -export const UnknownException: new(error: unknown, message?: string | undefined) => Cause.UnknownException = +export const UnknownException: new(cause: unknown, message?: string | undefined) => Cause.UnknownException = (function() { class UnknownException extends YieldableError { readonly _tag = "UnknownException" - constructor(readonly error: unknown, message?: string) { - super(message ?? (hasProperty(error, "message") && isString(error.message) ? error.message : void 0)) + readonly error: unknown + constructor(readonly cause: unknown, message?: string) { + super(message ?? "An unknown error occurred", { cause }) + this.error = cause } } Object.assign(UnknownException.prototype, {