mirror of
https://github.com/Effect-TS/effect.git
synced 2026-12-03 03:25:40 +08:00
Consolidate Effect.ignore and Effect.ignoreLogged (#1087)
This commit is contained in:
+36
-6
@@ -2,10 +2,40 @@
|
||||
|
||||
This directory contains specifications for all major features and enhancements in the Effect library, following a structured spec-driven development approach that ensures systematic planning, implementation, and validation.
|
||||
|
||||
## Contents
|
||||
## Index
|
||||
|
||||
- [cli-with-fallback-prompt.md](cli-with-fallback-prompt.md) - Add fallback prompt combinators for flags and positional arguments.
|
||||
- [cli-prompt-autocomplete.md](cli-prompt-autocomplete.md) - Add a typed filter auto-complete prompt for CLI selects.
|
||||
- [worker-error-reason-pattern.md](worker-error-reason-pattern.md) - Refactor WorkerError to use the reason pattern with structured reasons.
|
||||
- [http-client-error-reason-pattern.md](http-client-error-reason-pattern.md) - Refactor HttpClientError to a reason-based wrapper with per-reason classes.
|
||||
- [http-server-error-reason-pattern.md](http-server-error-reason-pattern.md) - Refactor HttpServerError to use the reason pattern with per-reason classes.
|
||||
- [Effect.ignore log option + remove ignoreLogged](./effect-ignore-log.md) - Add optional logging to `Effect.ignore` and remove `Effect.ignoreLogged`.
|
||||
|
||||
## Structure
|
||||
|
||||
Each feature specification follows a standardized structure within its own directory:
|
||||
|
||||
- **`instructions.md`** - Initial requirements capture and user story
|
||||
- **`requirements.md`** - Detailed functional and non-functional requirements
|
||||
- **`design.md`** - Technical design and architectural decisions
|
||||
- **`plan.md`** - Implementation roadmap with progress tracking
|
||||
|
||||
## Workflow
|
||||
|
||||
The specification-driven development process follows three key phases:
|
||||
|
||||
### 1. Specification Phase
|
||||
|
||||
Document requirements, design architecture, and create detailed implementation plans before beginning development work.
|
||||
|
||||
### 2. Implementation Phase
|
||||
|
||||
Execute the planned implementation following Effect library patterns and quality standards, with continuous validation against specifications.
|
||||
|
||||
### 3. Validation Phase
|
||||
|
||||
Verify that the final implementation meets all specified requirements and maintains Effect library standards for type safety, functional programming patterns, and comprehensive documentation.
|
||||
|
||||
---
|
||||
|
||||
Each feature specification ensures:
|
||||
|
||||
- **Systematic Development**: Structured approach from conception to completion
|
||||
- **Effect Library Compliance**: Adherence to functional programming patterns and type safety standards
|
||||
- **Quality Assurance**: Comprehensive validation including JSDoc compilation, testing, and documentation
|
||||
- **Progress Tracking**: Clear visibility into implementation status and completion criteria
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
# Effect.ignore log option + remove ignoreLogged
|
||||
|
||||
## Summary
|
||||
|
||||
Remove `Effect.ignoreLogged` and extend `Effect.ignore` with an optional logging
|
||||
configuration so callers can opt into logging failures while still discarding
|
||||
results.
|
||||
|
||||
## Background
|
||||
|
||||
`Effect.ignoreLogged` overlaps with `Effect.ignore`. We want a single API that
|
||||
supports optional logging while preserving the existing `ignore` default
|
||||
behavior (no logging). This is a breaking change because `ignoreLogged` is
|
||||
removed.
|
||||
|
||||
## Goals
|
||||
|
||||
- Provide `Effect.ignore` options with `log?: boolean | LogLevel`.
|
||||
- Preserve current `Effect.ignore` behavior when no options are supplied.
|
||||
- Log the full `Cause` using the current fiber log level when `log: true`.
|
||||
- Allow overriding the log level when `log` is a `LogLevel`.
|
||||
- Remove `Effect.ignoreLogged` from public and internal APIs.
|
||||
- Update docs to reflect the new API and removal.
|
||||
|
||||
## Non-goals
|
||||
|
||||
- No changes to logging infrastructure or log formatting.
|
||||
- No deprecation path for `ignoreLogged`.
|
||||
- No new logging helpers beyond `Effect.ignore` options.
|
||||
|
||||
## Requirements
|
||||
|
||||
### API
|
||||
|
||||
- Add overloads for `Effect.ignore` to accept options:
|
||||
- `Effect.ignore(self, options?)`
|
||||
- `Effect.ignore(options?)(self)`
|
||||
- Introduce a documented options type shape:
|
||||
- `{ readonly log?: boolean | LogLevel }`
|
||||
- `log?: boolean | LogLevel`:
|
||||
- `undefined` or `false`: do not log failures.
|
||||
- `true`: log the full `Cause` at the current fiber log level
|
||||
(equivalent to `logWithLevel()` today).
|
||||
- `LogLevel`: log the full `Cause` at the provided level.
|
||||
- `Effect.ignore` still returns `Effect<void, never, R>`.
|
||||
|
||||
### Behavior
|
||||
|
||||
- Success value is discarded as `void`.
|
||||
- Failures (including defects and interrupts) are discarded as `void`.
|
||||
- When logging is enabled, log the full `Cause` using existing logger behavior
|
||||
(`logWithLevel`), with no additional message formatting.
|
||||
|
||||
### Removal
|
||||
|
||||
- Remove `Effect.ignoreLogged` from:
|
||||
- internal implementation (`packages/effect/src/internal/effect.ts`)
|
||||
- public API (`packages/effect/src/Effect.ts`)
|
||||
- any docs referencing it
|
||||
|
||||
### Documentation
|
||||
|
||||
- Update `Effect.ignore` JSDoc to document `log` option and include an example.
|
||||
- Remove `ignoreLogged` JSDoc and any references to it in docs.
|
||||
- Add a migration note in docs if a suitable location exists (short guidance:
|
||||
use `Effect.ignore({ log: true })` or `Effect.ignore({ log: "Error" })`).
|
||||
|
||||
## Migration
|
||||
|
||||
- Replace `Effect.ignoreLogged(effect)` with:
|
||||
- `Effect.ignore(effect, { log: true })` or
|
||||
- `Effect.ignore({ log: true })(effect)`
|
||||
- For custom level: `Effect.ignore(effect, { log: "Error" })`.
|
||||
|
||||
## Testing
|
||||
|
||||
- Add tests that capture logs via a custom `Logger` layer:
|
||||
- `log` omitted/false: no logs emitted on failure.
|
||||
- `log: true`: emits one log with the current log level and the full `Cause`.
|
||||
- `log: <LogLevel>`: emits one log with the provided log level.
|
||||
- Use `it.effect` from `@effect/vitest` and `assert` (no `expect`).
|
||||
|
||||
## Validation
|
||||
|
||||
- `pnpm lint-fix`
|
||||
- `pnpm test <relevant test file>`
|
||||
- `pnpm check` (run `pnpm clean` then re-run if it fails)
|
||||
- `pnpm build`
|
||||
- `pnpm docgen`
|
||||
|
||||
## Acceptance Criteria
|
||||
|
||||
- `Effect.ignore` supports `log?: boolean | LogLevel` with behavior above.
|
||||
- `Effect.ignoreLogged` is removed from the codebase and docs.
|
||||
- Docs reflect the new API and include a clear example.
|
||||
- Tests cover logging enabled/disabled behavior.
|
||||
@@ -3734,6 +3734,8 @@ export const sandbox: <A, E, R>(
|
||||
* it succeeds or fails. This is useful when you only care about the side
|
||||
* effects of the effect and do not need to handle or process its outcome.
|
||||
*
|
||||
* Use the `log` option to emit the full {@link Cause} when the effect fails.
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* // Title: Using Effect.ignore to Discard Values
|
||||
@@ -3748,18 +3750,34 @@ export const sandbox: <A, E, R>(
|
||||
* const program = Effect.ignore(task)
|
||||
* ```
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* // Title: Logging failures while ignoring results
|
||||
* import { Effect } from "effect"
|
||||
*
|
||||
* const task = Effect.fail("Uh oh!")
|
||||
*
|
||||
* const program = Effect.ignore(task, { log: "Error" })
|
||||
* ```
|
||||
*
|
||||
* **Migration**
|
||||
*
|
||||
* Replace `Effect.ignoreLogged(self)` with `Effect.ignore(self, { log: true })`.
|
||||
*
|
||||
* @since 2.0.0
|
||||
* @category Error handling
|
||||
*/
|
||||
export const ignore: <A, E, R>(
|
||||
self: Effect<A, E, R>
|
||||
) => Effect<void, never, R> = internal.ignore
|
||||
|
||||
/**
|
||||
* @since 2.0.0
|
||||
* @category Error handling
|
||||
*/
|
||||
export const ignoreLogged: <A, E, R>(self: Effect<A, E, R>) => Effect<void, never, R> = internal.ignoreLogged
|
||||
export const ignore: <
|
||||
Arg extends Effect<any, any, any> | {
|
||||
readonly log?: boolean | LogLevel | undefined
|
||||
} | undefined
|
||||
>(
|
||||
effectOrOptions: Arg,
|
||||
options?: {
|
||||
readonly log?: boolean | LogLevel | undefined
|
||||
} | undefined
|
||||
) => [Arg] extends [Effect<infer _A, infer _E, infer _R>] ? Effect<void, never, _R>
|
||||
: <A, E, R>(self: Effect<A, E, R>) => Effect<void, never, R> = internal.ignore
|
||||
|
||||
/**
|
||||
* Apply an `ExecutionPlan` to the effect, which allows you to fallback to
|
||||
|
||||
@@ -2990,18 +2990,40 @@ export const eventually = <A, E, R>(self: Effect.Effect<A, E, R>): Effect.Effect
|
||||
catch_(self, (_) => flatMap(yieldNow, () => eventually(self)))
|
||||
|
||||
/** @internal */
|
||||
export const ignore = <A, E, R>(
|
||||
self: Effect.Effect<A, E, R>
|
||||
): Effect.Effect<void, never, R> => matchEffect(self, { onFailure: (_) => void_, onSuccess: (_) => void_ })
|
||||
|
||||
/** @internal */
|
||||
export const ignoreLogged = <A, E, R>(
|
||||
self: Effect.Effect<A, E, R>
|
||||
): Effect.Effect<void, never, R> =>
|
||||
matchCauseEffect(self, {
|
||||
onFailure: logWithLevel(),
|
||||
onSuccess: (_) => void_
|
||||
})
|
||||
export const ignore: <
|
||||
Arg extends Effect.Effect<any, any, any> | {
|
||||
readonly log?: boolean | LogLevel.LogLevel | undefined
|
||||
} | undefined
|
||||
>(
|
||||
effectOrOptions: Arg,
|
||||
options?: {
|
||||
readonly log?: boolean | LogLevel.LogLevel | undefined
|
||||
} | undefined
|
||||
) => [Arg] extends [Effect.Effect<infer _A, infer _E, infer _R>] ? Effect.Effect<void, never, _R>
|
||||
: <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<void, never, R> = dual(
|
||||
(args) => isEffect(args[0]),
|
||||
<A, E, R>(
|
||||
self: Effect.Effect<A, E, R>,
|
||||
options?: {
|
||||
readonly log?: boolean | LogLevel.LogLevel | undefined
|
||||
} | undefined
|
||||
): Effect.Effect<void, never, R> => {
|
||||
const log = options?.log
|
||||
if (log === undefined || log === false) {
|
||||
return matchEffect(self, { onFailure: (_) => void_, onSuccess: (_) => void_ })
|
||||
}
|
||||
const logEffect = logWithLevel(log === true ? undefined : log)
|
||||
return matchCauseEffect(self, {
|
||||
onFailure: (cause) => {
|
||||
const failure = causeFilterFail(cause)
|
||||
return Filter.isFail(failure)
|
||||
? andThen(logEffect(cause), failCause(failure.fail))
|
||||
: logEffect(cause)
|
||||
},
|
||||
onSuccess: (_) => void_
|
||||
})
|
||||
}
|
||||
)
|
||||
|
||||
/** @internal */
|
||||
export const option = <A, E, R>(
|
||||
|
||||
@@ -8,7 +8,10 @@ import {
|
||||
Exit,
|
||||
Fiber,
|
||||
Filter,
|
||||
Logger,
|
||||
type LogLevel,
|
||||
Option,
|
||||
References,
|
||||
Result,
|
||||
Schedule,
|
||||
Scope,
|
||||
@@ -17,6 +20,7 @@ import {
|
||||
} from "effect"
|
||||
import { constFalse, constTrue, pipe } from "effect/Function"
|
||||
import { TestClock } from "effect/testing"
|
||||
import { assertCauseFail } from "./utils/assert.ts"
|
||||
|
||||
class ATag extends ServiceMap.Service<ATag, "A">()("ATag") {}
|
||||
|
||||
@@ -1267,6 +1271,63 @@ describe("Effect", () => {
|
||||
}))
|
||||
})
|
||||
|
||||
describe("Effect.ignore", () => {
|
||||
type IgnoreOptions = { readonly log?: boolean | LogLevel.LogLevel }
|
||||
|
||||
const makeTestLogger = () => {
|
||||
const capturedLogs: Array<{
|
||||
readonly logLevel: LogLevel.LogLevel
|
||||
readonly cause: Cause.Cause<unknown>
|
||||
}> = []
|
||||
const testLogger = Logger.make<unknown, void>((options) => {
|
||||
capturedLogs.push({ logLevel: options.logLevel, cause: options.cause })
|
||||
})
|
||||
return { capturedLogs, testLogger }
|
||||
}
|
||||
|
||||
const runIgnore = (options?: IgnoreOptions, currentLogLevel: LogLevel.LogLevel = "Info") =>
|
||||
Effect.gen(function*() {
|
||||
const { capturedLogs, testLogger } = makeTestLogger()
|
||||
const program = options === undefined
|
||||
? Effect.fail("boom").pipe(Effect.ignore)
|
||||
: Effect.fail("boom").pipe(Effect.ignore(options))
|
||||
yield* program.pipe(
|
||||
Effect.provide(Logger.layer([testLogger])),
|
||||
Effect.provideService(References.MinimumLogLevel, "Trace"),
|
||||
Effect.provideService(References.CurrentLogLevel, currentLogLevel)
|
||||
)
|
||||
return capturedLogs
|
||||
})
|
||||
|
||||
it.effect("does not log when log is omitted", () =>
|
||||
Effect.gen(function*() {
|
||||
const logs = yield* runIgnore()
|
||||
assert.strictEqual(logs.length, 0)
|
||||
}))
|
||||
|
||||
it.effect("does not log when log is false", () =>
|
||||
Effect.gen(function*() {
|
||||
const logs = yield* runIgnore({ log: false })
|
||||
assert.strictEqual(logs.length, 0)
|
||||
}))
|
||||
|
||||
it.effect("logs with the current level when log is true", () =>
|
||||
Effect.gen(function*() {
|
||||
const logs = yield* runIgnore({ log: true }, "Warn")
|
||||
assert.strictEqual(logs.length, 1)
|
||||
assert.strictEqual(logs[0].logLevel, "Warn")
|
||||
assertCauseFail(logs[0].cause, "boom")
|
||||
}))
|
||||
|
||||
it.effect("logs with the provided level when log is a LogLevel", () =>
|
||||
Effect.gen(function*() {
|
||||
const logs = yield* runIgnore({ log: "Error" }, "Warn")
|
||||
assert.strictEqual(logs.length, 1)
|
||||
assert.strictEqual(logs[0].logLevel, "Error")
|
||||
assertCauseFail(logs[0].cause, "boom")
|
||||
}))
|
||||
})
|
||||
|
||||
describe("error handling", () => {
|
||||
class ErrorA extends Data.TaggedError("A") {}
|
||||
class ErrorB extends Data.TaggedError("B") {}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import type { Cause } from "effect"
|
||||
import { Equal, Option, Predicate, Result } from "effect"
|
||||
import { Cause, Equal, Option, Predicate, Result } from "effect"
|
||||
import * as Exit from "effect/Exit"
|
||||
import * as assert from "node:assert"
|
||||
import { assert as vassert } from "vitest"
|
||||
@@ -171,3 +170,11 @@ export function assertExitSuccess<A, E>(
|
||||
): asserts exit is Exit.Success<A, never> {
|
||||
deepStrictEqual(exit, Exit.succeed(expected))
|
||||
}
|
||||
|
||||
export function assertCauseFail<E>(
|
||||
cause: Cause.Cause<E>,
|
||||
expected: E,
|
||||
..._: Array<never>
|
||||
) {
|
||||
deepStrictEqual(cause, Cause.fail(expected))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user