Skip to content

Commit

Permalink
refactor built-in exceptions (#1671)
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart authored Nov 20, 2023
1 parent fc9bce6 commit c415248
Show file tree
Hide file tree
Showing 27 changed files with 602 additions and 381 deletions.
5 changes: 5 additions & 0 deletions .changeset/bright-seals-run.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": minor
---

support Promise in Effect.andThen and .tap
5 changes: 5 additions & 0 deletions .changeset/cyan-wolves-turn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": minor
---

add Cause.UnknownException and use it over `unknown`
5 changes: 5 additions & 0 deletions .changeset/young-flies-dance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": patch
---

move internal exceptions into core
112 changes: 102 additions & 10 deletions docs/modules/Cause.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ Added in v2.0.0
- [InterruptedException](#interruptedexception)
- [NoSuchElementException](#nosuchelementexception)
- [RuntimeException](#runtimeexception)
- [UnknownException](#unknownexception)
- [YieldableError](#yieldableerror)
- [originalError](#originalerror)
- [filtering](#filtering)
- [filter](#filter)
Expand Down Expand Up @@ -93,6 +95,8 @@ Added in v2.0.0
- [Parallel (interface)](#parallel-interface)
- [RuntimeException (interface)](#runtimeexception-interface)
- [Sequential (interface)](#sequential-interface)
- [UnknownException (interface)](#unknownexception-interface)
- [YieldableError (interface)](#yieldableerror-interface)
- [refinements](#refinements)
- [isCause](#iscause)
- [isDieType](#isdietype)
Expand All @@ -105,6 +109,7 @@ Added in v2.0.0
- [isParallelType](#isparalleltype)
- [isRuntimeException](#isruntimeexception)
- [isSequentialType](#issequentialtype)
- [isUnknownException](#isunknownexception)
- [rendering](#rendering)
- [pretty](#pretty)
- [sequencing](#sequencing)
Expand All @@ -124,6 +129,8 @@ Added in v2.0.0
- [NoSuchElementExceptionTypeId (type alias)](#nosuchelementexceptiontypeid-type-alias)
- [RuntimeExceptionTypeId](#runtimeexceptiontypeid)
- [RuntimeExceptionTypeId (type alias)](#runtimeexceptiontypeid-type-alias)
- [UnknownExceptionTypeId](#unknownexceptiontypeid)
- [UnknownExceptionTypeId (type alias)](#unknownexceptiontypeid-type-alias)
- [utils](#utils)
- [Cause (namespace)](#cause-namespace)
- [Variance (interface)](#variance-interface)
Expand Down Expand Up @@ -324,6 +331,31 @@ export declare const RuntimeException: (message?: string) => RuntimeException
Added in v2.0.0
## UnknownException
Represents a checked exception which occurs when an unknown error is thrown, such as
from a rejected promise.
**Signature**
```ts
export declare const UnknownException: (error: unknown, message?: string | undefined) => UnknownException
```
Added in v2.0.0
## YieldableError
Represents a generic checked exception which occurs at runtime.
**Signature**
```ts
export declare const YieldableError: new (message?: string | undefined) => YieldableError
```
Added in v2.0.0
## originalError
Returns the original, unproxied, instance of a thrown error
Expand Down Expand Up @@ -780,10 +812,9 @@ provided to a method.
**Signature**

```ts
export interface IllegalArgumentException {
export interface IllegalArgumentException extends YieldableError {
readonly _tag: "IllegalArgumentException"
readonly [IllegalArgumentExceptionTypeId]: IllegalArgumentExceptionTypeId
readonly message?: string
}
```

Expand Down Expand Up @@ -812,10 +843,9 @@ Represents a checked exception which occurs when a `Fiber` is interrupted.
**Signature**

```ts
export interface InterruptedException {
export interface InterruptedException extends YieldableError {
readonly _tag: "InterruptedException"
readonly [InterruptedExceptionTypeId]: InterruptedExceptionTypeId
readonly message?: string
}
```

Expand All @@ -829,10 +859,9 @@ Represents a checked exception which occurs when attempting to construct a
**Signature**

```ts
export interface InvalidPubSubCapacityException {
export interface InvalidPubSubCapacityException extends YieldableError {
readonly _tag: "InvalidPubSubCapacityException"
readonly [InvalidPubSubCapacityExceptionTypeId]: InvalidPubSubCapacityExceptionTypeId
readonly message?: string
}
```

Expand All @@ -846,10 +875,9 @@ unable to be found.
**Signature**

```ts
export interface NoSuchElementException {
export interface NoSuchElementException extends YieldableError {
readonly _tag: "NoSuchElementException"
readonly [NoSuchElementExceptionTypeId]: NoSuchElementExceptionTypeId
readonly message?: string
}
```

Expand Down Expand Up @@ -885,10 +913,9 @@ Represents a generic checked exception which occurs at runtime.
**Signature**

```ts
export interface RuntimeException {
export interface RuntimeException extends YieldableError {
readonly _tag: "RuntimeException"
readonly [RuntimeExceptionTypeId]: RuntimeExceptionTypeId
readonly message?: string
}
```

Expand Down Expand Up @@ -916,6 +943,38 @@ export interface Sequential<out E> extends Cause.Variance<E>, Equal.Equal, Pipea

Added in v2.0.0

## UnknownException (interface)

Represents a checked exception which occurs when an unknown error is thrown, such as
from a rejected promise.

**Signature**

```ts
export interface UnknownException extends YieldableError {
readonly _tag: "UnknownException"
readonly [UnknownExceptionTypeId]: UnknownExceptionTypeId
readonly error: unknown
}
```

Added in v2.0.0

## YieldableError (interface)

**Signature**

```ts
export interface YieldableError extends Data.Case, Pipeable, Readonly<Error> {
readonly [Effect.EffectTypeId]: Effect.Effect.VarianceStruct<never, this, never>
readonly [Stream.StreamTypeId]: Effect.Effect.VarianceStruct<never, this, never>
readonly [Sink.SinkTypeId]: Sink.Sink.VarianceStruct<never, this, unknown, never, never>
readonly [Channel.ChannelTypeId]: Channel.Channel.VarianceStruct<never, unknown, unknown, unknown, this, never, never>
}
```

Added in v2.0.0

# refinements

## isCause
Expand Down Expand Up @@ -1060,6 +1119,19 @@ export declare const isSequentialType: <E>(self: Cause<E>) => self is Sequential
Added in v2.0.0
## isUnknownException
Returns `true` if the specified value is an `UnknownException`, `false`
otherwise.
**Signature**
```ts
export declare const isUnknownException: (u: unknown) => u is UnknownException
```
Added in v2.0.0
# rendering
## pretty
Expand Down Expand Up @@ -1238,6 +1310,26 @@ export type RuntimeExceptionTypeId = typeof RuntimeExceptionTypeId
Added in v2.0.0
## UnknownExceptionTypeId
**Signature**
```ts
export declare const UnknownExceptionTypeId: typeof UnknownExceptionTypeId
```
Added in v2.0.0
## UnknownExceptionTypeId (type alias)
**Signature**
```ts
export type UnknownExceptionTypeId = typeof UnknownExceptionTypeId
```
Added in v2.0.0
# utils
## Cause (namespace)
Expand Down
28 changes: 2 additions & 26 deletions docs/modules/Data.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ Added in v2.0.0
- [Case (interface)](#case-interface)
- [Data (type alias)](#data-type-alias)
- [TaggedEnum (type alias)](#taggedenum-type-alias)
- [YieldableError (interface)](#yieldableerror-interface)
- [utils](#utils)
- [Case (namespace)](#case-namespace)
- [Constructor (interface)](#constructor-interface)
Expand Down Expand Up @@ -89,7 +88,7 @@ export declare const Error: new <A extends Record<string, any>>(
args: Types.Equals<Omit<A, keyof Equal.Equal>, {}> extends true
? void
: { readonly [P in keyof A as P extends keyof Equal.Equal ? never : P]: A[P] }
) => YieldableError & Readonly<A>
) => Cause.YieldableError & Readonly<A>
```
Added in v2.0.0
Expand Down Expand Up @@ -157,7 +156,7 @@ export declare const TaggedError: <Tag extends string>(
args: Types.Equals<Omit<A, keyof Equal.Equal>, {}> extends true
? void
: { readonly [P in keyof A as P extends "_tag" | keyof Equal.Equal ? never : P]: A[P] }
) => YieldableError & { readonly _tag: Tag } & Readonly<A>
) => Cause.YieldableError & { readonly _tag: Tag } & Readonly<A>
```
Added in v2.0.0
Expand Down Expand Up @@ -477,29 +476,6 @@ export type TaggedEnum<A extends Record<string, Record<string, any>> & UntaggedC
Added in v2.0.0
## YieldableError (interface)
**Signature**
```ts
export interface YieldableError extends Case, Pipeable, Readonly<Error> {
readonly [Effectable.EffectTypeId]: Effect.Effect.VarianceStruct<never, this, never>
readonly [Effectable.StreamTypeId]: Effect.Effect.VarianceStruct<never, this, never>
readonly [Effectable.SinkTypeId]: Sink.Sink.VarianceStruct<never, this, unknown, never, never>
readonly [Effectable.ChannelTypeId]: Channel.Channel.VarianceStruct<
never,
unknown,
unknown,
unknown,
this,
never,
never
>
}
```

Added in v2.0.0

# utils
## Case (namespace)
Expand Down
52 changes: 42 additions & 10 deletions docs/modules/Effect.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -2595,7 +2595,7 @@ thrown exceptions into typed failed effects creating with `Effect.fail`.
**Signature**
```ts
export declare const try: { <A, E>(options: { readonly try: LazyArg<A>; readonly catch: (error: unknown) => E; }): Effect<never, E, A>; <A>(evaluate: LazyArg<A>): Effect<never, unknown, A>; }
export declare const try: { <A, E>(options: { readonly try: LazyArg<A>; readonly catch: (error: unknown) => E; }): Effect<never, E, A>; <A>(evaluate: LazyArg<A>): Effect<never, Cause.UnknownException, A>; }
```
Added in v2.0.0
Expand Down Expand Up @@ -2665,7 +2665,7 @@ export declare const tryPromise: {
readonly try: (signal: AbortSignal) => Promise<A>
readonly catch: (error: unknown) => E
}): Effect<never, E, A>
<A>(try_: (signal: AbortSignal) => Promise<A>): Effect<never, unknown, A>
<A>(try_: (signal: AbortSignal) => Promise<A>): Effect<never, Cause.UnknownException, A>
}
```
Expand Down Expand Up @@ -5170,20 +5170,36 @@ export declare const andThen: {
f: (a: NoInfer<A>) => X
): <R, E>(
self: Effect<R, E, A>
) => [X] extends [Effect<infer R1, infer E1, infer A1>] ? Effect<R | R1, E | E1, A1> : Effect<R, E, X>
) => [X] extends [Effect<infer R1, infer E1, infer A1>]
? Effect<R | R1, E | E1, A1>
: [X] extends [Promise<infer A1>]
? Effect<R, Cause.UnknownException | E, A1>
: Effect<R, E, X>
<X>(
f: X
): <R, E, A>(
self: Effect<R, E, A>
) => [X] extends [Effect<infer R1, infer E1, infer A1>] ? Effect<R | R1, E | E1, A1> : Effect<R, E, X>
) => [X] extends [Effect<infer R1, infer E1, infer A1>]
? Effect<R | R1, E | E1, A1>
: [X] extends [Promise<infer A1>]
? Effect<R, Cause.UnknownException | E, A1>
: Effect<R, E, X>
<A, R, E, X>(
self: Effect<R, E, A>,
f: (a: NoInfer<A>) => X
): [X] extends [Effect<infer R1, infer E1, infer A1>] ? Effect<R | R1, E | E1, A1> : Effect<R, E, X>
): [X] extends [Effect<infer R1, infer E1, infer A1>]
? Effect<R | R1, E | E1, A1>
: [X] extends [Promise<infer A1>]
? Effect<R, Cause.UnknownException | E, A1>
: Effect<R, E, X>
<A, R, E, X>(
self: Effect<R, E, A>,
f: X
): [X] extends [Effect<infer R1, infer E1, infer A1>] ? Effect<R | R1, E | E1, A1> : Effect<R, E, X>
): [X] extends [Effect<infer R1, infer E1, infer A1>]
? Effect<R | R1, E | E1, A1>
: [X] extends [Promise<infer A1>]
? Effect<R, Cause.UnknownException | E, A1>
: Effect<R, E, X>
}
```
Expand Down Expand Up @@ -5334,20 +5350,36 @@ export declare const tap: {
f: (a: NoInfer<A>) => X
): <R, E>(
self: Effect<R, E, A>
) => [X] extends [Effect<infer R1, infer E1, infer _A1>] ? Effect<R | R1, E | E1, A> : Effect<R, E, A>
) => [X] extends [Effect<infer R1, infer E1, infer _A1>]
? Effect<R | R1, E | E1, A>
: [X] extends [Promise<infer _A1>]
? Effect<R, Cause.UnknownException | E, A>
: Effect<R, E, A>
<X>(
f: X
): <R, E, A>(
self: Effect<R, E, A>
) => [X] extends [Effect<infer R1, infer E1, infer _A1>] ? Effect<R | R1, E | E1, A> : Effect<R, E, A>
) => [X] extends [Effect<infer R1, infer E1, infer _A1>]
? Effect<R | R1, E | E1, A>
: [X] extends [Promise<infer _A1>]
? Effect<R, Cause.UnknownException | E, A>
: Effect<R, E, A>
<A, R, E, X>(
self: Effect<R, E, A>,
f: (a: NoInfer<A>) => X
): [X] extends [Effect<infer R1, infer E1, infer _A1>] ? Effect<R | R1, E | E1, A> : Effect<R, E, A>
): [X] extends [Effect<infer R1, infer E1, infer _A1>]
? Effect<R | R1, E | E1, A>
: [X] extends [Promise<infer _A1>]
? Effect<R, Cause.UnknownException | E, A>
: Effect<R, E, A>
<A, R, E, X>(
self: Effect<R, E, A>,
f: X
): [X] extends [Effect<infer R1, infer E1, infer _A1>] ? Effect<R | R1, E | E1, A> : Effect<R, E, A>
): [X] extends [Effect<infer R1, infer E1, infer _A1>]
? Effect<R | R1, E | E1, A>
: [X] extends [Promise<infer _A1>]
? Effect<R, Cause.UnknownException | E, A>
: Effect<R, E, A>
}
```
Expand Down
Loading

0 comments on commit c415248

Please sign in to comment.