Skip to content

Commit 1ef3d6f

Browse files
hive-botclaude
andcommitted
fix: use the libuv errno for the synthetic EEXIST error
os.constants.errno.EEXIST is the CRT value on Windows (17), but Node's fs errors carry libuv's UV_EEXIST (-4075 there). Look up the errno via util.getSystemErrorMap() so it matches a real open(outputFile, 'wx') error. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 94adbfa commit 1ef3d6f

1 file changed

Lines changed: 20 additions & 2 deletions

File tree

‎src/deeplClient.ts‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,28 @@ import { ArgumentError, DeepLError, GlossaryNotFoundError } from './errors';
9898
import { GlossaryEntries } from './glossaryEntries';
9999
import { IncomingMessage } from 'http';
100100
import * as fs from 'fs';
101-
import * as os from 'os';
102101
import * as path from 'path';
103102
import * as util from 'util';
104103

104+
/**
105+
* Returns the libuv errno that Node's fs errors carry for the given error name (e.g. -4075 for
106+
* EEXIST on Windows, -17 on Linux), or undefined if it cannot be determined.
107+
*/
108+
function uvErrno(name: string): number | undefined {
109+
const getSystemErrorMap = (
110+
util as unknown as { getSystemErrorMap?: () => Map<number, [string, string]> }
111+
).getSystemErrorMap;
112+
if (!getSystemErrorMap) {
113+
return undefined;
114+
}
115+
for (const [errno, [errName]] of getSystemErrorMap()) {
116+
if (errName === name) {
117+
return errno;
118+
}
119+
}
120+
return undefined;
121+
}
122+
105123
function spokenTermsEntriesToJson(entries: SpokenTermsEntries): Record<string, unknown> {
106124
// Terms are trimmed, then validated and sent trimmed. Terms are sent newline-separated, so a
107125
// newline inside a term would split it
@@ -1670,7 +1688,7 @@ export class DeepLClient extends Translator {
16701688
throw Object.assign(
16711689
new Error(`EEXIST: file already exists, open '${outputFile}'`),
16721690
{
1673-
errno: -os.constants.errno.EEXIST,
1691+
errno: uvErrno('EEXIST'),
16741692
code: 'EEXIST',
16751693
syscall: 'open',
16761694
path: outputFile,

0 commit comments

Comments
 (0)