refactor(core): own environment error wrapping in WorkspaceEnvironment
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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) =>
|
||||
|
||||
Reference in New Issue
Block a user