1.4.5 • Published 4 days ago

effect-errors v1.4.5

Weekly downloads
-
License
MIT
Repository
github
Last release
4 days ago

effect-errors

Open in Visual Studio Code npm bundle size Github workflow Quality Gate Status Maintainability Rating Security Rating Reliability Rating Coverage Lines of Code Technical Debt Code Smells Bugs Vulnerabilities Duplicated Lines (%) Last commit

Some sort of POC to improve the way Effect reports errors in a dev env 🤔

example

⚡ So how does it work?

Had to re-export runSync and runPromise to apply prettyPrint function on the cause returned by a catchAll.

So using it would look like this :

import { runPromise } from 'effect-errors';

await runPromise(
  Effect.gen(function* () {
    // ...
  }),
);

You can also directly import the prettyPrint function to do whatever with it if you want 🤷

import { prettyPrint } from 'effect-errors';

await Effect.runPromise(
  pipe(
    Effect.gen(function* () {
      // ...
    }),
    Effect.sandbox,
    Effect.catchAll((e) => {
      console.error(prettyPrint(e));

      return Effect.fail('❌ runPromise failure') as never;
    }),
  ),
);

Signature is the following:

const prettyPrint: <E>(cause: Cause<E>, options?: PrettyPrintOptions) => string;

PrettyPrintOptions allows you to tweak the following:

enabled - Whether pretty printing is enabled or not

default: true

stripCwd - Whether spans and stacktrace should contain absolute or relative paths

default: false (absolute paths)

reverseSpans - Whether spans order should reversed (entry point first instead of inner callee first)

default: true (entry point first)

⚡ How should I raise errors?

The best way is to use either SchemaError or TaggedError.

🔶 SchemaError

Declaring the error could look like this:

import * as Schema from '@effect/schema/Schema';

export class FileNotFoundError extends Schema.TaggedError<SchemaError>()(
  'FileNotFound',
  {
    cause: Schema.optional(Schema.unknown),
  },
) {}

You would then raise a FileNotFoundError to the error channel like this:

Effect.tryPromise({
  try: () => ...,
  catch: (e) => new FileNotFoundError({ cause: e }),
});

// or raising directly
Effect.fail(new FileNotFoundError({ cause: "Oh no!" }));

🔶 TaggedError

export class UserNotFoundError extends TaggedError('UserNotFound')<{
  cause?: unknown;
}> {}

You would then raise a UserNotFoundError to the error channel like this:

Effect.tryPromise({
  try: () => ...,
  catch: (e) => new UserNotFoundError({ cause: e }),
});

// or raising directly
Effect.fail(new UserNotFoundError({ cause: "User does not exist" }));

🔶 Plain object

Alternatively, you can use a plain object with a _tag and message attribute, but you won't get any stacktrace if you use this method:

Effect.fail({ _tag: 'SucksToBeMe', message: 'Yeah...' });

⚡ Capturing errors data

You might want to apply your own logic to reported errors data; for example if you want to display errors in html. You can do so using captureErrors. The function has the following signature:

interface ErrorSpan {
  name: string;
  attributes: ReadonlyMap<string, unknown>;
  status: SpanStatus;
}

interface ErrorData {
  errorType: unknown;
  message: unknown;
  stack?: string[];
  effectStacktrace?: string[];
  spans?: ErrorSpan[];
  isPlainString: boolean;
}

interface CapturedErrors {
  interrupted: boolean;
  errors: ErrorData[];
}

interface CaptureErrorsOptions {
  reverseSpans?: boolean;
  stripCwd?: boolean;
}

const captureErrors: <E>(
  cause: Cause<E>,
  { reverseSpans, stripCwd }?: CaptureErrorsOptions,
) => CapturedErrors;

You can use captureErrors like so:

import { captureErrors } from 'effect-errors';

await Effect.runPromise(
  pipe(
    effect,
    Effect.catchAll((e) => {
      const data = captureErrors(e);

      // ...
    }),
  ),
);

⚡ examples

🔶 error logging - runPromise / runSync

I wrote some examples for fun and giggles. You can run them using:

bun run-examples

🔶 Custom display for errors - captureErrors

You can check this example using remix error boundaries.

1.4.5

4 days ago

1.4.4

4 days ago

1.4.3

5 days ago

1.4.2

5 days ago

1.4.0

6 days ago

1.3.12

11 days ago

1.3.11

12 days ago

1.3.10

22 days ago

1.3.9

25 days ago

1.3.8

25 days ago

1.3.7

27 days ago

1.3.6

27 days ago

1.3.5

27 days ago

1.3.4

1 month ago

1.3.3

1 month ago

1.3.2

1 month ago

1.3.1

1 month ago

1.3.0

2 months ago

1.2.18

2 months ago

1.2.19

2 months ago

1.2.20

2 months ago

1.2.23

2 months ago

1.2.24

2 months ago

1.2.21

2 months ago

1.2.22

2 months ago

1.2.17

2 months ago

1.2.12

2 months ago

1.2.13

2 months ago

1.2.11

2 months ago

1.2.16

2 months ago

1.2.14

2 months ago

1.2.15

2 months ago

1.2.8

2 months ago

1.2.7

2 months ago

1.2.6

2 months ago

1.2.5

2 months ago

1.2.4

2 months ago

1.2.3

2 months ago

1.2.9

2 months ago

1.2.10

2 months ago

1.2.0

3 months ago

1.1.1

3 months ago

1.1.0

3 months ago

1.0.9

3 months ago

1.0.8

3 months ago

1.0.7

3 months ago

1.1.5

3 months ago

1.0.6

3 months ago

1.1.4

3 months ago

1.2.2

3 months ago

1.1.3

3 months ago

1.2.1

3 months ago

1.1.2

3 months ago

1.0.5

3 months ago

1.0.4

3 months ago

1.0.3

3 months ago