Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/deno-write-copy-errors.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/platform-deno": patch
---

Preserve high-level filesystem error context for `writeFile` and normalize Deno `AlreadyExists` errors from `copy`.
27 changes: 25 additions & 2 deletions packages/effect/test/FileSystem.test-utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { assert, expect, it } from "@effect/vitest"
import { Array } from "effect"
import { Array, Result } from "effect"
import * as Effect from "effect/Effect"
import * as Fs from "effect/FileSystem"
import type * as Layer from "effect/Layer"
Expand Down Expand Up @@ -137,8 +137,11 @@ export const testLayer = <E>(layer: Layer.Layer<Fs.FileSystem, E>, options: Test
const fs = yield* Fs.FileSystem
const path = yield* fs.makeTempFile()

yield* fs.writeFileString(path, "data", { flag: "r" }).pipe(Effect.flip)
const error = yield* fs.writeFileString(path, "data", { flag: "r" }).pipe(Effect.flip)

assert(error.reason._tag !== "BadArgument")
assert.strictEqual(error.reason.method, "writeFile")
assert.strictEqual(error.reason.pathOrDescriptor, path)
assert.strictEqual(yield* fs.readFileString(path), "")
})))

Expand All @@ -165,6 +168,26 @@ export const testLayer = <E>(layer: Layer.Layer<Fs.FileSystem, E>, options: Test
assert.strictEqual(yield* fs.readFileString(path), "first")
})))

it("copy with overwrite false preserves an existing destination", () =>
runPromise(Effect.gen(function*() {
const fs = yield* Fs.FileSystem
const root = yield* fs.makeTempDirectory()
const source = `${root}/source.txt`
const destination = `${root}/destination.txt`
yield* fs.writeFileString(source, "source")
yield* fs.writeFileString(destination, "destination")

const result = yield* Effect.result(fs.copy(source, destination, { overwrite: false }))

if (Result.isFailure(result)) {
assert(result.failure.reason._tag === "AlreadyExists")
assert.strictEqual(result.failure.reason.method, "copy")
assert.strictEqual(result.failure.reason.pathOrDescriptor, source)
}
assert.strictEqual(yield* fs.readFileString(source), "source")
assert.strictEqual(yield* fs.readFileString(destination), "destination")
})))

it("should track the cursor position when reading", () =>
runPromise(Effect.gen(function*() {
const fs = yield* Fs.FileSystem
Expand Down
9 changes: 8 additions & 1 deletion packages/platform-deno/src/DenoFileSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,14 @@ const writeFile: FileSystem.FileSystem["writeFile"] = (path, data, options) => {
}
return Effect.acquireUseRelease(
tryPromise("writeFile", path, () => Deno.open(path, openOptions(flag, options?.mode))),
(file) => new FileImpl(file, flag.startsWith("a")).writeAll(data),
(file) =>
new FileImpl(file, flag.startsWith("a")).writeAll(data).pipe(
Effect.mapError((error) =>

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use instanceof

error.reason._tag !== "BadArgument"
? PlatformError.systemError({ ...error.reason, method: "writeFile", pathOrDescriptor: path })
: error
)
),
(file) => close(file, "writeFile", path)
)
}
Expand Down
3 changes: 3 additions & 0 deletions packages/platform-deno/src/internal/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ export const handleError = (
let tag: SystemErrorTag = "Unknown"

switch (denoError?.name) {
case "AlreadyExists":
tag = "AlreadyExists"
break
case "NotCapable":
tag = "PermissionDenied"
break
Expand Down
1 change: 1 addition & 0 deletions packages/platform-deno/test/internal/error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ describe("handleError", () => {
const cases: ReadonlyArray<readonly [error: Error, tag: SystemErrorTag]> = [
[withCode(new Deno.errors.NotFound(), "ENOENT"), "NotFound"],
[withCode(new Deno.errors.NotADirectory(), "ENOTDIR"), "BadResource"],
[new Deno.errors.AlreadyExists(), "AlreadyExists"],
[withCode(new Deno.errors.AlreadyExists(), "EEXIST"), "AlreadyExists"],
[withCode(new Deno.errors.IsADirectory(), "EISDIR"), "BadResource"],
[withCode(new Deno.errors.PermissionDenied(), "EACCES"), "PermissionDenied"],
Expand Down
Loading