Skip to content

Commit

Permalink
Centralize error messages for bugs (#2503)
Browse files Browse the repository at this point in the history
gcanti authored Apr 12, 2024

Verified

This commit was signed with the committer’s verified signature.
1 parent da22adc commit 41c8102
Showing 12 changed files with 37 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/blue-humans-rest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": patch
---

Centralize error messages for bugs
2 changes: 1 addition & 1 deletion packages/effect/src/Duration.ts
Original file line number Diff line number Diff line change
@@ -128,7 +128,7 @@ export const decode = (input: DurationInput): Duration => {
}
}
}
throw new Error("Invalid duration input")
throw new Error("Invalid DurationInput")
}

/**
8 changes: 5 additions & 3 deletions packages/effect/src/List.ts
Original file line number Diff line number Diff line change
@@ -924,6 +924,8 @@ export const take: {
*/
export const toChunk = <A>(self: List<A>): Chunk.Chunk<A> => Chunk.fromIterable(self)

const getExpectedListToBeNonEmptyErrorMessage = "Expected List to be non-empty"

/**
* Unsafely returns the first element of the specified `List`.
*
@@ -932,7 +934,7 @@ export const toChunk = <A>(self: List<A>): Chunk.Chunk<A> => Chunk.fromIterable(
*/
export const unsafeHead = <A>(self: List<A>): A => {
if (isNil(self)) {
throw new Error("Expected List to be non-empty")
throw new Error(getExpectedListToBeNonEmptyErrorMessage)
}
return self.head
}
@@ -945,7 +947,7 @@ export const unsafeHead = <A>(self: List<A>): A => {
*/
export const unsafeLast = <A>(self: List<A>): A => {
if (isNil(self)) {
throw new Error("Expected List to be non-empty")
throw new Error(getExpectedListToBeNonEmptyErrorMessage)
}
let these = self
let scout = self.tail
@@ -964,7 +966,7 @@ export const unsafeLast = <A>(self: List<A>): A => {
*/
export const unsafeTail = <A>(self: List<A>): List<A> => {
if (isNil(self)) {
throw new Error("Expected List to be non-empty")
throw new Error(getExpectedListToBeNonEmptyErrorMessage)
}
return self.tail
}
3 changes: 2 additions & 1 deletion packages/effect/src/TestAnnotation.ts
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ import type * as Fiber from "./Fiber.js"
import { pipe } from "./Function.js"
import * as Hash from "./Hash.js"
import * as HashSet from "./HashSet.js"
import { getBugErrorMessage } from "./internal/errors.js"
import type * as MutableRef from "./MutableRef.js"
import { hasProperty } from "./Predicate.js"
import type * as SortedSet from "./SortedSet.js"
@@ -96,7 +97,7 @@ export const compose = <A>(
if (Either.isLeft(left) && Either.isRight(right)) {
return right
}
throw new Error("BUG: TestAnnotation.compose - please report an issue at https://github.com/Effect-TS/effect/issues")
throw new Error(getBugErrorMessage("TestAnnotation.compose"))
}

/**
5 changes: 3 additions & 2 deletions packages/effect/src/internal/cause.ts
Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@ import type { Predicate, Refinement } from "../Predicate.js"
import * as ReadonlyArray from "../ReadonlyArray.js"
import type { ParentSpan, Span } from "../Tracer.js"
import type { NoInfer } from "../Types.js"
import { getBugErrorMessage } from "./errors.js"
import * as OpCodes from "./opCodes/cause.js"

// -----------------------------------------------------------------------------
@@ -604,7 +605,7 @@ const flattenCauseLoop = (
causes = sequential
flattened = updated
}
throw new Error("BUG: Cause.flattenCauseLoop - please report an issue at https://github.com/Effect-TS/effect/issues")
throw new Error(getBugErrorMessage("Cause.flattenCauseLoop"))
}

// -----------------------------------------------------------------------------
@@ -740,7 +741,7 @@ const evaluateCause = (
}
}
}
throw new Error("BUG: Cause.evaluateCauseLoop - please report an issue at https://github.com/Effect-TS/effect/issues")
throw new Error(getBugErrorMessage("Cause.evaluateCauseLoop"))
}

// -----------------------------------------------------------------------------
3 changes: 2 additions & 1 deletion packages/effect/src/internal/core.ts
Original file line number Diff line number Diff line change
@@ -41,6 +41,7 @@ import * as internalCause from "./cause.js"
import * as deferred from "./deferred.js"
import * as internalDiffer from "./differ.js"
import { effectVariance, StructuralCommitPrototype } from "./effectable.js"
import { getBugErrorMessage } from "./errors.js"
import type * as FiberRuntime from "./fiberRuntime.js"
import type * as fiberScope from "./fiberScope.js"
import * as DeferredOpCodes from "./opCodes/deferred.js"
@@ -469,7 +470,7 @@ export const custom: {
break
}
default: {
throw new Error("Bug, you're not supposed to end up here")
throw new Error(getBugErrorMessage("you're not supposed to end up here"))
}
}
return wrapper
7 changes: 7 additions & 0 deletions packages/effect/src/internal/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* @since 2.0.0
*/

/** @internal */
export const getBugErrorMessage = (message: string) =>
`BUG: ${message} - please report an issue at https://github.com/Effect-TS/effect/issues`
2 changes: 1 addition & 1 deletion packages/effect/src/internal/hashMap.ts
Original file line number Diff line number Diff line change
@@ -270,7 +270,7 @@ export const unsafeGet = Dual.dual<
>(2, (self, key) => {
const element = getHash(self, key, Hash.hash(key))
if (Option.isNone(element)) {
throw new Error("Error: Expected map to contain key")
throw new Error("Expected map to contain key")
}
return element.value
})
8 changes: 5 additions & 3 deletions packages/effect/src/internal/matcher.ts
Original file line number Diff line number Diff line change
@@ -508,7 +508,7 @@ export const orElseAbsurd = <I, R, RA, A, Pr>(
self: Matcher<I, R, RA, A, Pr>
): [Pr] extends [never] ? (input: I) => Unify<A> : Unify<A> =>
orElse(() => {
throw new Error("absurd")
throw new Error("effect/Match/orElseAbsurd: absurd")
})(self)

/** @internal */
@@ -565,6 +565,8 @@ export const option: <I, F, R, A, Pr>(
})
}) as any

const getExhaustiveAbsurdErrorMessage = "effect/Match/exhaustive: absurd"

/** @internal */
export const exhaustive: <I, F, A, Pr>(
self: Matcher<I, F, never, A, Pr>
@@ -578,7 +580,7 @@ export const exhaustive: <I, F, A, Pr>(
return toEither.right
}

throw new Error("@effect/match: exhaustive absurd")
throw new Error(getExhaustiveAbsurdErrorMessage)
}

return (u: I): A => {
@@ -589,6 +591,6 @@ export const exhaustive: <I, F, A, Pr>(
return result.right as any
}

throw new Error("@effect/match: exhaustive absurd")
throw new Error(getExhaustiveAbsurdErrorMessage)
}
}) as any
3 changes: 2 additions & 1 deletion packages/effect/src/internal/schedule/intervals.ts
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ import { dual, pipe } from "../../Function.js"
import * as Option from "../../Option.js"
import * as Interval from "../../ScheduleInterval.js"
import type * as Intervals from "../../ScheduleIntervals.js"
import { getBugErrorMessage } from "../errors.js"

/** @internal */
const IntervalsSymbolKey = "effect/ScheduleIntervals"
@@ -109,7 +110,7 @@ const unionLoop = (
that = Chunk.tailNonEmpty(that)
}
} else {
throw new Error("BUG: Intervals.unionLoop - please report an issue at https://github.com/Effect-TS/effect/issues")
throw new Error(getBugErrorMessage("Intervals.unionLoop"))
}
}
return make(pipe(acc, Chunk.prepend(interval), Chunk.reverse))
2 changes: 1 addition & 1 deletion packages/effect/src/internal/trie.ts
Original file line number Diff line number Diff line change
@@ -420,7 +420,7 @@ export const unsafeGet = dual<
>(2, (self, key) => {
const element = get(self, key)
if (Option.isNone(element)) {
throw new Error("Error: Expected trie to contain key")
throw new Error("Expected trie to contain key")
}
return element.value
})
6 changes: 3 additions & 3 deletions packages/effect/test/Duration.test.ts
Original file line number Diff line number Diff line change
@@ -41,9 +41,9 @@ describe("Duration", () => {
expect(Duration.decode([500, 123456789])).toEqual(Duration.nanos(500123456789n))
expect(Duration.decode([-500, 123456789])).toEqual(Duration.zero)

expect(() => Duration.decode("1.5 secs" as any)).toThrowError(new Error("Invalid duration input"))
expect(() => Duration.decode(true as any)).toThrowError(new Error("Invalid duration input"))
expect(() => Duration.decode({} as any)).toThrowError(new Error("Invalid duration input"))
expect(() => Duration.decode("1.5 secs" as any)).toThrowError(new Error("Invalid DurationInput"))
expect(() => Duration.decode(true as any)).toThrowError(new Error("Invalid DurationInput"))
expect(() => Duration.decode({} as any)).toThrowError(new Error("Invalid DurationInput"))
})

it("decodeUnknown", () => {

0 comments on commit 41c8102

Please sign in to comment.