Skip to content

Commit 0308d03

Browse files
committed
update
1 parent cabbd72 commit 0308d03

4 files changed

Lines changed: 393 additions & 41 deletions

File tree

packages/filesystem/baidu/baidu.test.ts

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,13 @@ describe("BaiduFileSystem", () => {
100100
expect(String((requestSpy.mock.calls[2][1] as RequestInit).body)).toContain("rtype=0");
101101
});
102102

103-
it("writer should surface Baidu createOnly rejection as conflict", async () => {
103+
it("writer should surface Baidu createOnly exists rejection as conflict", async () => {
104104
const fs = new BaiduFileSystem("/apps", "token");
105105
vi.spyOn(fs, "list").mockResolvedValue([]);
106-
vi.spyOn(fs, "request").mockResolvedValueOnce({ errno: -8, errmsg: "file exists" });
106+
vi.spyOn(fs, "request").mockResolvedValueOnce({
107+
errno: -8,
108+
errmsg: "file exists",
109+
});
107110

108111
const writer = await fs.create("test.txt", { createOnly: true });
109112

@@ -113,6 +116,38 @@ describe("BaiduFileSystem", () => {
113116
});
114117
});
115118

119+
it("writer should surface Baidu createOnly duplicate name rejection as conflict", async () => {
120+
const fs = new BaiduFileSystem("/apps", "token");
121+
vi.spyOn(fs, "list").mockResolvedValue([]);
122+
vi.spyOn(fs, "request").mockResolvedValueOnce({
123+
errno: 31061,
124+
errmsg: "duplicate name",
125+
});
126+
127+
const writer = await fs.create("test.txt", { createOnly: true });
128+
129+
await expect(writer.write("content")).rejects.toMatchObject({
130+
provider: "baidu",
131+
conflict: true,
132+
code: "31061",
133+
});
134+
});
135+
136+
it("writer should not classify non-exists Baidu createOnly errno as conflict", async () => {
137+
const fs = new BaiduFileSystem("/apps", "token");
138+
vi.spyOn(fs, "list").mockResolvedValue([]);
139+
vi.spyOn(fs, "request").mockResolvedValueOnce({
140+
errno: 111,
141+
errmsg: "quota exceeded",
142+
});
143+
144+
const writer = await fs.create("test.txt", { createOnly: true });
145+
146+
await expect(writer.write("content")).rejects.not.toMatchObject({
147+
conflict: true,
148+
});
149+
});
150+
116151
it("writer should reject expectedDigest when remote digest changed", async () => {
117152
const fs = new BaiduFileSystem("/apps", "token");
118153
vi.spyOn(fs, "list").mockResolvedValue([

packages/filesystem/baidu/rw.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,16 +135,21 @@ export class BaiduFileWriter implements FileWriter {
135135
}
136136

137137
private throwCreateOnlyConflict(data: any): void {
138-
if (!this.opts?.createOnly) {
138+
if (!this.opts?.createOnly || !this.isBaiduCreateConflict(data)) {
139139
return;
140140
}
141-
throw fileConflictError("baidu", `File already exists or createOnly write was rejected: ${this.path}`, {
141+
throw fileConflictError("baidu", `File already exists: ${this.path}`, {
142142
status: 409,
143143
code: String(data.errno),
144144
raw: data,
145145
});
146146
}
147147

148+
private isBaiduCreateConflict(data: any): boolean {
149+
const errno = String(data?.errno);
150+
return errno === "-8" || errno === "31061";
151+
}
152+
148153
private async checkWritePrecondition(): Promise<void> {
149154
if (!this.opts?.expectedDigest && !this.opts?.createOnly) {
150155
return;

0 commit comments

Comments
 (0)