refactor(core): own environment error wrapping in WorkspaceEnvironment

This commit is contained in:
Kit Langton
2026-08-06 17:09:46 -04:00
parent dee3f88737
commit 13a415b68a
3 changed files with 28 additions and 12 deletions
@@ -18,6 +18,24 @@ export class NotFoundError extends Schema.TaggedErrorClass<NotFoundError>()("Wor
path: Schema.String,
}) {}
/**
* Wrap one driver promise, translating the driver's not-found signal into the
* environment error vocabulary so drivers and fakes never construct it ad hoc.
*/
export const tryOperation = <A>(input: {
readonly operation: string
readonly path: string
readonly run: () => Promise<A>
readonly isNotFound: (cause: unknown) => boolean
}): Effect.Effect<A, Error | NotFoundError> =>
Effect.tryPromise({
try: input.run,
catch: (cause) =>
input.isNotFound(cause)
? new NotFoundError({ path: input.path })
: new Error({ operation: input.operation, path: input.path, cause }),
})
export interface FileInfo {
readonly type: FileSystem.File.Type
}
+5 -6
View File
@@ -39,12 +39,11 @@ export const directoryEnvironment = (
spawn: ChildProcessSpawner["Service"]["spawn"],
): WorkspaceEnvironment.Interface => {
const wrap = <A>(operation: string, path: string, run: () => Promise<A>) =>
Effect.tryPromise({
try: run,
catch: (cause) =>
(cause as NodeJS.ErrnoException).code === "ENOENT"
? new WorkspaceEnvironment.NotFoundError({ path })
: new WorkspaceEnvironment.Error({ operation, path, cause }),
WorkspaceEnvironment.tryOperation({
operation,
path,
run,
isNotFound: (cause) => (cause as NodeJS.ErrnoException).code === "ENOENT",
})
return WorkspaceEnvironment.make({
directory: root,
+5 -6
View File
@@ -95,12 +95,11 @@ const FILE_TYPE = { file: "File", directory: "Directory", symlink: "SymbolicLink
const files = (sandbox: Sandbox): WorkspaceEnvironment.Files => {
const wrap = <A>(operation: string, path: string, run: () => Promise<A>) =>
Effect.tryPromise({
try: run,
catch: (cause) =>
cause instanceof SandboxFilesystemNotFoundError
? new WorkspaceEnvironment.NotFoundError({ path })
: new WorkspaceEnvironment.Error({ operation, path, cause }),
WorkspaceEnvironment.tryOperation({
operation,
path,
run,
isNotFound: (cause) => cause instanceof SandboxFilesystemNotFoundError,
})
return {
stat: (path) =>