-
Notifications
You must be signed in to change notification settings - Fork 3.5k
[codex] Preserve auth HTTP failure diagnostics #3419
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
juliusmarminge
wants to merge
2
commits into
codex/redact-dpop-request-target
from
codex/auth-http-diagnostics
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| import { describe, expect, it } from "@effect/vitest"; | ||
| import { EnvironmentInternalError } from "@t3tools/contracts"; | ||
| import * as Cause from "effect/Cause"; | ||
| import * as Effect from "effect/Effect"; | ||
| import * as Exit from "effect/Exit"; | ||
| import * as Logger from "effect/Logger"; | ||
| import * as Option from "effect/Option"; | ||
| import * as Schema from "effect/Schema"; | ||
| import * as HttpServerRequest from "effect/unstable/http/HttpServerRequest"; | ||
|
|
||
| import * as AuthHttp from "./http.ts"; | ||
|
|
||
| const encodeEnvironmentInternalError = Schema.encodeUnknownSync(EnvironmentInternalError); | ||
|
|
||
| const loggerLayer = (messages: Array<unknown>) => | ||
| Logger.layer( | ||
| [ | ||
| Logger.make(({ message }) => { | ||
| messages.push(message); | ||
| }), | ||
| ], | ||
| { mergeWithExisting: false }, | ||
| ); | ||
|
|
||
| describe("auth http diagnostics", () => { | ||
| it.effect("logs and encodes bounded internal diagnostics without exposing secrets", () => { | ||
| const messages: Array<unknown> = []; | ||
| const cause = new Error("credential=secret-value"); | ||
|
|
||
| return Effect.gen(function* () { | ||
| const error = yield* Effect.flip( | ||
| AuthHttp.failEnvironmentInternal("browser_session_cookie_failed", cause), | ||
| ); | ||
|
|
||
| expect(error).toBeInstanceOf(AuthHttp.EnvironmentHttpInternalError); | ||
| expect(error.failureTag).toBe("Error"); | ||
| expect(error.message).toBe( | ||
| "Environment API operation failed (browser_session_cookie_failed).", | ||
| ); | ||
| expect(encodeEnvironmentInternalError(error)).toEqual({ | ||
| _tag: "EnvironmentInternalError", | ||
| code: "internal_error", | ||
| reason: "browser_session_cookie_failed", | ||
| traceId: error.traceId, | ||
| }); | ||
|
|
||
| expect(messages).toEqual([ | ||
| [ | ||
| "environment api operation failed", | ||
| { | ||
| reason: "browser_session_cookie_failed", | ||
| traceId: error.traceId, | ||
| failureTag: "Error", | ||
| reasonCount: 1, | ||
| failureCount: 1, | ||
| defectCount: 0, | ||
| interruptionCount: 0, | ||
| }, | ||
| ], | ||
| ]); | ||
| }).pipe(Effect.provide(loggerLayer(messages))); | ||
| }); | ||
|
|
||
| it.effect("logs request failures without serializing their Error or Cause values", () => { | ||
| const messages: Array<unknown> = []; | ||
| const request = HttpServerRequest.fromWeb( | ||
| new Request("https://environment.example.test/api/auth/session"), | ||
| ); | ||
| const requestCause = Cause.combine( | ||
| Cause.fail(new Error("credential=secret-value")), | ||
| Cause.die(new Error("stderr=private-value")), | ||
| ); | ||
|
|
||
| return Effect.gen(function* () { | ||
| const exit = yield* Effect.exit( | ||
| Effect.scoped( | ||
| Effect.gen(function* () { | ||
| yield* AuthHttp.annotateEnvironmentRequest("auth.session"); | ||
| return yield* Effect.failCause(requestCause); | ||
| }), | ||
| ).pipe(Effect.provideService(HttpServerRequest.HttpServerRequest, request)), | ||
| ); | ||
|
|
||
| expect(Exit.isFailure(exit)).toBe(true); | ||
| expect(messages).toEqual([ | ||
| [ | ||
| "environment api request failed", | ||
| { | ||
| endpoint: "auth.session", | ||
| traceId: "unavailable", | ||
| failureTag: "Error", | ||
| reasonCount: 2, | ||
| failureCount: 1, | ||
| defectCount: 1, | ||
| interruptionCount: 0, | ||
| }, | ||
| ], | ||
| ]); | ||
| }).pipe(Effect.provide(loggerLayer(messages))); | ||
| }); | ||
|
|
||
| it.effect("re-propagates nested interruption causes without logging a synthetic 500", () => { | ||
| const messages: Array<unknown> = []; | ||
| const interruption = Cause.interrupt(); | ||
| const cause = new Error("cancelled", { cause: interruption }); | ||
|
|
||
| return Effect.gen(function* () { | ||
| const exit = yield* Effect.exit(AuthHttp.failEnvironmentInternal("internal_error", cause)); | ||
|
|
||
| expect(Exit.isFailure(exit)).toBe(true); | ||
| if (Exit.isFailure(exit)) { | ||
| expect(Cause.hasInterruptsOnly(exit.cause)).toBe(true); | ||
| expect(Option.isNone(Cause.findErrorOption(exit.cause))).toBe(true); | ||
| } | ||
| expect(messages).toEqual([]); | ||
| }).pipe(Effect.provide(loggerLayer(messages))); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.