GeezerVison is showing github.com — refactor: errors are Schema.TaggedError, not Data.TaggedError or native Error by juliusmarminge · Pull Request #31216 · pingdotgg/t3code
refactor: errors are Schema.TaggedError, not Data.TaggedError or native Error
#31216
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
Uh oh!
There was an error while loading. Please reload this page.
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter
Filter by extension
.json
(1)
.ts
(72)
.tsx
(13)
All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Hide whitespace
Diff view
Hide whitespace
refactor: errors are Schema.TaggedError, not Data.TaggedError or nati…
…ve Error
Enable the Effect language-service rules preferSchemaTaggedError and
extendsNativeError as errors, and convert every hit to Schema.TaggedError.
instanceof checks on converted classes become module-level Schema.is guards.
Co-Authored-By: Claude Opus 5.5 <[email protected] >
Loading branch information
Uh oh!
There was an error while loading. Please reload this page.
commit 5018d69087222598db62a98a74ea9c52d4b5a018
There are no files selected for viewing
Original file line number
Diff line number
Diff line change
Expand Up
@@ -26,7 +26,6 @@ import {
} from "@t3tools/contracts" ;
import { SshHttpBridgeError } from "@t3tools/ssh/errors" ;
import { resolveLoopbackSshHttpBaseUrl } from "@t3tools/ssh/tunnel" ;
import * as Data from "effect/Data" ;
import * as Effect from "effect/Effect" ;
import * as Schema from "effect/Schema" ;
Expand All
@@ -35,10 +34,12 @@ import * as DesktopIpc from "../DesktopIpc.ts";
import * as DesktopSshEnvironment from "../../ssh/DesktopSshEnvironment.ts" ;
import * as DesktopSshPasswordPrompts from "../../ssh/DesktopSshPasswordPrompts.ts" ;
type DesktopSshEnvironmentRequestOperation =
| "fetch-environment-descriptor"
| "bootstrap-bearer-session"
| "fetch-session-state" ;
const DesktopSshEnvironmentRequestOperation = Schema . Literals ( [
"fetch-environment-descriptor" ,
"bootstrap-bearer-session" ,
"fetch-session-state" ,
] ) ;
type DesktopSshEnvironmentRequestOperation = typeof DesktopSshEnvironmentRequestOperation . Type ;
type DesktopSshEnvironmentRequestCause = RemoteEnvironmentAuthError | SshHttpBridgeError ;
Expand All
@@ -47,12 +48,13 @@ const isEnvironmentInternalError = Schema.is(EnvironmentInternalError);
const isEnvironmentOperationForbiddenError = Schema . is ( EnvironmentOperationForbiddenError ) ;
const isEnvironmentRequestInvalidError = Schema . is ( EnvironmentRequestInvalidError ) ;
const isEnvironmentScopeRequiredError = Schema . is ( EnvironmentScopeRequiredError ) ;
const isRemoteEnvironmentAuthUndeclaredStatusError = Schema . is (
RemoteEnvironmentAuthUndeclaredStatusError ,
) ;
const isSshHttpBridgeError = Schema . is ( SshHttpBridgeError ) ;
function readSshHttpStatus ( cause : DesktopSshEnvironmentRequestCause ) : number | null {
if (
cause instanceof RemoteEnvironmentAuthUndeclaredStatusError ||
cause instanceof SshHttpBridgeError
) {
if ( isRemoteEnvironmentAuthUndeclaredStatusError ( cause ) || isSshHttpBridgeError ( cause ) ) {
return cause . status ?? null ;
}
if ( isEnvironmentRequestInvalidError ( cause ) ) {
Expand All
@@ -73,13 +75,14 @@ function readSshHttpStatus(cause: DesktopSshEnvironmentRequestCause): number | n
return null ;
}
export class DesktopSshEnvironmentRequestError extends Data . TaggedError (
export class DesktopSshEnvironmentRequestError extends Schema . TaggedError < DesktopSshEnvironmentRequestError > ( ) (
"DesktopSshEnvironmentRequestError" ,
) < {
readonly operation : DesktopSshEnvironmentRequestOperation ;
readonly cause : DesktopSshEnvironmentRequestCause ;
readonly sshHttpStatus : number | null ;
} > {
{
operation : DesktopSshEnvironmentRequestOperation ,
cause : Schema . Defect ( ) ,
sshHttpStatus : Schema . NullOr ( Schema . Number ) ,
} ,
) {
// The web client matches "[ssh_http:500]" to explain version-skewed SSH pairing.
override get message ( ) {
const prefix = this . sshHttpStatus === null ? "" : `[ssh_http:${ this . sshHttpStatus } ] ` ;
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -1872,7 +1872,9 @@ it.effect("capture fails closed while snapshots are disabled", () => {
it . effect ( "points capture setup failures at SnapShot settings" , ( ) => {
vi . stubEnv ( "XDG_SESSION_TYPE" , "wayland" ) ;
linuxCaptureMock . mockRejectedValueOnce ( new CaptureSetupRequiredError ( "Install the helper." ) ) ;
linuxCaptureMock . mockRejectedValueOnce (
new CaptureSetupRequiredError ( { state : "Install the helper." } ) ,
) ;
return Effect . scoped (
Effect . gen ( function * ( ) {
const service = yield * DesktopSnapShot . make ;
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -94,6 +94,8 @@ import {
import * as HostProcess from "@t3tools/shared/HostProcess" ;
import { readImageDimensions } from "@t3tools/shared/imageDimensions" ;
const isCaptureSetupRequiredError = Schema . is ( CaptureSetupRequiredError ) ;
const MAX_CAPTURE_WIDTH = 2_560 ;
const MAX_CAPTURE_HEIGHT = 1_600 ;
const SHORTCUT_COOLDOWN_NS = 200_000_000n ;
Expand Down
Expand Up
@@ -180,7 +182,7 @@ const shortcutFailure = (cause: unknown) => new SnapShotShortcutError({ cause })
function captureFailure ( cause : unknown , captureId ?: string ) : DesktopSnapShotError {
if ( isDesktopSnapShotError ( cause ) ) return cause ;
const operation = cause instanceof CaptureSetupRequiredError ? "setup-required" : "capture" ;
const operation = isCaptureSetupRequiredError ( cause ) ? "setup-required" : "capture" ;
return new DesktopSnapShotError ( { operation, captureId, cause } ) ;
}
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -171,11 +171,11 @@ export async function captureHyprlandWindow(
let state = await setup . state ( ) ;
if ( state . status === "update-required" ) {
await setup . perform ( "install-hyprland-helper" ) . catch ( ( cause : unknown ) => {
throw new CaptureSetupRequiredError ( state . message , { cause } ) ;
throw new CaptureSetupRequiredError ( { state : state . message , cause } ) ;
} ) ;
state = await setup . state ( ) ;
}
if ( state . status !== "ready" ) throw new CaptureSetupRequiredError ( state . message ) ;
if ( state . status !== "ready" ) throw new CaptureSetupRequiredError ( { state : state . message } ) ;
const executable = hyprlandCaptureExecutable ( paths ) ;
const directory = await NodeFSP . mkdtemp ( NodePath . join ( NodeOS . tmpdir ( ) , "t3-hyprland-capture-" ) ) ;
const cleanup = ( ) => NodeFSP . rm ( directory , { recursive : true , force : true } ) ;
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -215,7 +215,7 @@ export async function captureKdeWindow(
options?: { readonly flash: boolean; readonly animate: boolean },
): Promise<LinuxWindowSnapshot> {
const state = await new KdeCaptureSetup(paths).state();
if (state.status !== "ready") throw new CaptureSetupRequiredError(state.message);
if (state.status !== "ready") throw new CaptureSetupRequiredError({ state: state .message } );
const { executable } = kdeCapturePaths(paths);
const directory = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "t3-kde-capture-"));
let retained = false;
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -2,15 +2,19 @@
import * as NodeFSP from "node:fs/promises" ;
import * as NodeURL from "node:url" ;
import type { SnapShotKeyChord } from "@t3tools/contracts" ;
import * as Schema from "effect/Schema" ;
import { nativeImage } from "electron" ;
// Keep dbus-next out of desktop detection and PNG reads in the main process.
export const HYPRLAND_CAPTURE_ACTION = "capture-window" ;
export class CaptureSetupRequiredError extends Error {
constructor ( state : string , options ?: ErrorOptions ) {
super ( `${ state } Open Settings → SnapShots to continue setup.` , options ) ;
export class CaptureSetupRequiredError extends Schema . TaggedError < CaptureSetupRequiredError > ( ) (
"CaptureSetupRequiredError" ,
{ state : Schema . String , cause : Schema . optional ( Schema . Defect ( ) ) } ,
) {
override get message ( ) : string {
return `${ this . state } Open Settings → SnapShots to continue setup.` ;
}
}
export const NIRI_CAPTURE_PATH = "/com/t3tools/SnapShot" ;
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -23,6 +23,7 @@ import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
import * as Path from "effect/Path";
import * as Schema from "effect/Schema";
import * as HttpClient from "effect/http/HttpClient";
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
Expand Down
Expand Up
@@ -74,11 +75,13 @@ function discoverDesktopSshHostsEffect(input?: { readonly homeDir?: string }) {
return discoverSshHosts(input ?? {});
}
const isSshPasswordPromptError = Schema.is(SshPasswordPromptError);
export function isDesktopSshPasswordPromptCancellation(
error: unknown,
): error is SshPasswordPromptError {
return (
error instanceof SshPasswordPromptError &&
isSshPasswordPromptError( error) &&
DesktopSshPasswordPrompts.isDesktopSshPasswordPromptCancellation(error.cause)
);
}
Expand Down
Original file line number
Diff line number
Diff line change
@@ -1,3 +1,4 @@
import * as Schema from "effect/Schema" ;
import * as Dialog from "./ui/Dialog" ;
import { useContext , useEffect , useEffectEvent , useState } from "react" ;
import { Platform } from "react-native" ;
Expand All
@@ -21,15 +22,17 @@ export type {
ResolvedFilePreviewSource ,
} from "./FilePreviewModal.types" ;
class PreviewResolutionError extends Error {
constructor (
override readonly cause : unknown ,
readonly fallbackMessage : string ,
) {
super ( fallbackMessage , { cause } ) ;
class PreviewResolutionError extends Schema . TaggedError < PreviewResolutionError > ( ) (
"PreviewResolutionError" ,
{ cause : Schema . Defect ( ) , fallbackMessage : Schema . String } ,
) {
override get message ( ) : string {
return this . fallbackMessage ;
}
}
const isPreviewResolutionError = Schema . is ( PreviewResolutionError ) ;
interface ResolvedPreviewUri {
readonly uri : string ;
readonly dispose ?: ( ) => void ;
Expand All
@@ -42,7 +45,10 @@ async function resolvePreviewUri(
if ( "uri" in item ) return { uri : item . uri } ;
if ( "attachment" in item ) {
return loadLocalAttachmentPreview ( item . attachment , signal ) . catch ( ( error : unknown ) => {
throw new PreviewResolutionError ( error , "Attach the file again and retry." ) ;
throw new PreviewResolutionError ( {
cause : error ,
fallbackMessage : "Attach the file again and retry." ,
} ) ;
} ) ;
}
// Refresh expired URLs before handing them to Quick Look or ACTION_VIEW, which retain them.
Expand All
@@ -52,10 +58,11 @@ async function resolvePreviewUri(
return value ;
} )
. catch ( ( error : unknown ) => {
throw new PreviewResolutionError (
error ,
"Reconnect to this environment and try again. The file may have been moved or deleted." ,
) ;
throw new PreviewResolutionError ( {
cause : error ,
fallbackMessage :
"Reconnect to this environment and try again. The file may have been moved or deleted." ,
} ) ;
} ) ;
return { uri : url + ( item . srcFragment ?? "" ) } ;
}
Expand Down
Expand Up
@@ -99,9 +106,9 @@ function ResolvedFilePreview(props: {
if ( progressiveGallery && index !== selectedIndex ) return settle ( index , null ) ;
controller . abort ( ) ;
onResolutionError (
error instanceof PreviewResolutionError
isPreviewResolutionError ( error )
? error
: new PreviewResolutionError ( error , "Try again." ) ,
: new PreviewResolutionError ( { cause : error , fallbackMessage : "Try again." } ) ,
) ;
} ,
) ;
Expand Down
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,5 @@
import * as Digest from "effect/crypto/Digest";
import * as SecureRandom from "effect/crypto/SecureRandom";
import * as Data from "effect/Data";
import * as Effect from "effect/Effect";
import * as Base64Url from "effect/encoding/Base64Url";
import * as Hex from "effect/encoding/Hex";
Expand All
@@ -13,10 +12,10 @@ import { DpopProof } from "@t3tools/client-runtime/relay";
import { DpopPublicJwk } from "@t3tools/shared/dpopCommon";
import * as Layer from "effect/Layer";
export class CloudDpopError extends Data .TaggedError( "CloudDpopError")< {
readonly message: string;
readonly cause?: unknown;
}> {}
export class CloudDpopError extends Schema .TaggedError<CloudDpopError>()( "CloudDpopError", {
message: Schema.String,
cause: Schema.optional(Schema.Defect()),
}) {}
function cloudDpopError(message: string) {
return (cause: unknown) => new CloudDpopError({ message, cause });
Expand Down
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,3 @@
import * as Data from "effect/Data";
import * as Effect from "effect/Effect";
import * as Schema from "effect/Schema";
import { HttpClient } from "effect/http";
Expand Down
Expand Up
@@ -26,11 +25,14 @@ function readRelayUrl(): string | null {
return resolveCloudPublicConfig().relay.url;
}
export class CloudEnvironmentLinkError extends Data.TaggedError("CloudEnvironmentLinkError")<{
readonly message: string;
readonly cause?: unknown;
readonly traceId?: string;
}> {}
export class CloudEnvironmentLinkError extends Schema.TaggedError<CloudEnvironmentLinkError>()(
"CloudEnvironmentLinkError",
{
message: Schema.String,
cause: Schema.optional(Schema.Defect()),
traceId: Schema.optional(Schema.String),
},
) {}
const isEnvironmentCloudApiError = Schema.is(
Schema.Union([
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -12,6 +12,7 @@ import {
hostPreviewMimeTypeFromExtension ,
} from "@t3tools/shared/filePreview" ;
import { LegendList } from "@legendapp/list/react-native" ;
import * as Schema from "effect/Schema" ;
import { fetch } from "expo/fetch" ;
import { File } from "expo-file-system" ;
import { useNavigation , usePreventRemove } from "@react-navigation/native" ;
Expand Down
Expand Up
@@ -68,11 +69,12 @@ interface ZipArchive {
readonly fileCount : number ;
}
interface ZipFailure {
readonly title : string ;
readonly detail : string ;
readonly retry : boolean ;
}
const ZipFailure = Schema . Struct ( {
title : Schema . String ,
detail : Schema . String ,
retry : Schema . Boolean ,
} ) ;
type ZipFailure = typeof ZipFailure . Type ;
const SAVE_ZIP_HINT = "Save the ZIP to open it in another app." ;
const TOO_LARGE : ZipFailure = {
Expand All
@@ -91,12 +93,16 @@ const NETWORK: ZipFailure = {
retry : true ,
} ;
class ZipFailureError extends Error {
constructor ( readonly failure : ZipFailure ) {
super ( failure . title ) ;
class ZipFailureError extends Schema . TaggedError < ZipFailureError > ( ) ( "ZipFailureError" , {
failure : ZipFailure ,
} ) {
override get message ( ) : string {
return this . failure . title ;
}
}
const isZipFailureError = Schema . is ( ZipFailureError ) ;
function errorMessage ( cause : unknown , fallback : string ) {
return cause instanceof Error ? cause . message : fallback ;
}
Expand All
@@ -113,7 +119,7 @@ async function readArchiveBody(
const { value, done } = await reader . read ( ) ;
if ( done ) break ;
size += value . byteLength ;
if ( size > ARCHIVE_MAX_BYTES ) throw new ZipFailureError ( TOO_LARGE ) ;
if ( size > ARCHIVE_MAX_BYTES ) throw new ZipFailureError ( { failure : TOO_LARGE } ) ;
chunks . push ( value ) ;
onProgress ( size ) ;
}
Expand All
@@ -135,9 +141,11 @@ async function openArchive(bytes: ArrayBuffer): Promise<ZipArchive> {
files = await boundedZip ( bytes ) ;
} catch ( cause ) {
throw new ZipFailureError ( {
title : "Couldn't browse this ZIP" ,
detail : `${ errorMessage ( cause , "It may be incomplete or damaged." ) } ${ SAVE_ZIP_HINT } ` ,
retry : false ,
failure : {
title : "Couldn't browse this ZIP" ,
detail : `${ errorMessage ( cause , "It may be incomplete or damaged." ) } ${ SAVE_ZIP_HINT } ` ,
retry : false ,
} ,
} ) ;
}
const entries = [ ...files ] . map ( ( [ path , file ] ) : ZipEntry => {
Expand Down
Expand Up
@@ -187,15 +195,15 @@ function useZipArchive(input: {
// oxlint-disable-next-line react/set-state-in-effect -- A new archive load resets its progress.
setState ( { status : "loading" , receivedBytes : 0 , timeLeft : null } ) ;
void ( async ( ) => {
if ( sizeBytes > ARCHIVE_MAX_BYTES ) throw new ZipFailureError ( TOO_LARGE ) ;
if ( sizeBytes > ARCHIVE_MAX_BYTES ) throw new ZipFailureError ( { failure : TOO_LARGE } ) ;
if ( isLocal ) {
const file = new File ( uri ) ;
if ( ( file . size ?? 0 ) > ARCHIVE_MAX_BYTES ) throw new ZipFailureError ( TOO_LARGE ) ;
if ( ( file . size ?? 0 ) > ARCHIVE_MAX_BYTES ) throw new ZipFailureError ( { failure : TOO_LARGE } ) ;
return openArchive ( await file . arrayBuffer ( ) ) ;
}
const url = ( await refresh ( ) ) ?? uri ;
const response = await fetch ( url , { signal : abort . signal } ) ;
if ( ! response . ok || ! response . body ) throw new ZipFailureError ( NETWORK ) ;
if ( ! response . ok || ! response . body ) throw new ZipFailureError ( { failure : NETWORK } ) ;
const bytes = await readArchiveBody ( response . body , ( receivedBytes ) =>
setState ( {
status : "loading" ,
Expand All
@@ -217,7 +225,7 @@ function useZipArchive(input: {
if ( ! abort . signal . aborted )
setState ( {
status : "failed" ,
failure : cause instanceof ZipFailureError ? cause . failure : NETWORK ,
failure : isZipFailureError ( cause ) ? cause . failure : NETWORK ,
} ) ;
} ) ;
return ( ) => abort . abort ( ) ;
Expand Down
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,6 @@
import * as Data from "effect/Data";
import * as Effect from "effect/Effect";
import * as Schema from "effect/Schema";
import { Atom } from "effect/reactivity";
import { resolveHighlightLanguage } from "../../lib/highlightLanguage";
Expand All
@@ -15,9 +16,10 @@ export interface SourceHighlightInput {
class SourceHighlightCacheKey extends Data.Class<SourceHighlightInput> {}
class SourceHighlightError extends Data.TaggedError("SourceHighlightError")<{
readonly cause: unknown;
}> {}
class SourceHighlightError extends Schema.TaggedError<SourceHighlightError>()(
"SourceHighlightError",
{ cause: Schema.Defect() },
) {}
// Highlighting is synchronous, so a cold atom resolves on its first read. The
// family keeps results for files the user moves between.
Expand Down
Loading
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.