Skip to content

Commit 9f0b90c

Browse files
committed
fix(js): pyerrors.oserr: new catchJsErrAndSetErrno; add defval for raiseErrno,raiseErrnoWithPath
1 parent 3402fb8 commit 9f0b90c

File tree

2 files changed

+41
-35
lines changed

2 files changed

+41
-35
lines changed

src/pylib/Lib/errno_impl/errnoUtils.nim

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,13 @@ template setErrno0* = {.noSideEffect.}:
3030
else:
3131
errno = 0
3232

33-
template getErrno*(): cint = {.noSideEffect.}:
33+
proc getErrno*(): cint = {.noSideEffect.}:
3434
bind errno, staticErrno
35-
var res: cint
3635
block:
3736
when nimvm:
38-
res = staticErrno
37+
result = staticErrno
3938
else:
40-
res = errno
41-
res
39+
result = errno
4240

4341
template isErr*(E: untyped): bool =
4442
bind getErrno

src/pylib/pyerrors/oserr.nim

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ when InJs:
1414

1515
import ../io_abc
1616
import ../private/backendMark
17+
import ../Lib/errno_impl/errnoUtils
1718
export OSErrorCode
1819
type
1920
FileNotFoundError* = object of OSError
@@ -62,8 +63,9 @@ else:
6263
func newOSErrorWithMsg(err: OSErrorCode, msg: string): owned(ref OSError) =
6364
(ref OSError)(errorCode: err.int32, msg: msg)
6465

65-
func raiseExcWithPath*(fp: PathLike, exc: typedesc, err: OSErrorCode) =
66-
raise newException(exc, fp.osErrorMsgWithPath(err))
66+
proc raiseExcWithPath*(fp: PathLike, exc: typedesc, err: OSErrorCode,
67+
osErrorMsgCb: proc = osErrorMsg) =
68+
raise newException(exc, fp.osErrorMsgWithPath(err, osErrorMsgCb))
6769

6870
func raiseExcWithPath*(fp: PathLike, exc: typedesc, err: OSErrorCode, additionalInfo: string) =
6971
var msg = fp.osErrorMsgWithPath(err)
@@ -94,7 +96,7 @@ when defined(nimscript):
9496
ErrExist = NON_ERR_CODE
9597
ErrNoent = NON_ERR_CODE
9698
ErrIsdir = NON_ERR_CODE
97-
elif InJs: discard
99+
elif InJs: discard # those are defined in ./jsoserr
98100
else:
99101

100102
when defined(windows):
@@ -169,38 +171,47 @@ template tryOsOp*(p: PathLike, body) =
169171
170172
when InJs:
171173
proc errnoMsgOSErr(errnoCode: OSErrorCode): string = jsErrnoMsg(errnoCode)
172-
proc errnoMsg*(errnoCode: cint): string = jsErrnoMsg(errnoCode.OSErrorCode)
174+
proc errnoMsg*(errno: cint): string = jsErrnoMsg(errno.OSErrorCode)
173175
elif not weirdTarget:
174176
proc c_strerror(code: cint): cstring{.importc: "strerror", header: "<string.h>".}
175177
176178
func errnoMsgOSErr(errnoCode: OSErrorCode): string = $c_strerror(errnoCode.cint)
177179
178-
func errnoMsg*(errnoCode: cint): string = $c_strerror(errnoCode)
180+
func errnoMsg*(errno: cint): string = $c_strerror(errno)
179181
180182
181-
proc newErrnoErrT[E: OSError](errnoCode: cint, additionalInfo = ""): owned(ref E) =
182-
result = (ref E)(errorCode: errnoCode.int32, msg: errnoMsg(errnoCode))
183+
proc newErrnoErrT[E: OSError](errno=getErrno(), additionalInfo = ""): owned(ref E) =
184+
result = (ref E)(errorCode: errno.int32, msg: errnoMsg(errno))
183185
if additionalInfo.len > 0:
184186
if result.msg.len > 0 and result.msg[^1] != '\n': result.msg.add '\n'
185187
result.msg.add "Additional info: "
186188
result.msg.add additionalInfo
187189
# don't add trailing `.` etc, which negatively impacts "jump to file" in IDEs.
188190
if result.msg == "":
189191
result.msg = "unknown OS error"
190-
proc newErrnoErr(errnoCode: cint, additionalInfo = ""): owned(ref OSError) =
191-
newErrnoErrT[OSError](errnoCode, additionalInfo)
192+
proc newErrnoErr(errno=getErrno(), additionalInfo = ""): owned(ref OSError) =
193+
newErrnoErrT[OSError](errno, additionalInfo)
192194
193-
194-
proc raiseErrno*(errno: cint; additionalInfo = "") =
195+
proc raiseErrno*(errno=getErrno(); additionalInfo = "") =
195196
## may raise OSError only
196197
raise newErrnoErr(errno, additionalInfo)
197198
198-
proc raiseErrnoWithPath*[T](p: PathLike[T]; errno: cint) =
199+
proc newErrnoErr*[E: OSError](additionalInfo: string): owned(ref E) =
200+
newErrnoErrT[E](additionalInfo = additionalInfo)
201+
202+
proc raiseErrnoT*[E: OSError](additionalInfo: string) =
203+
raise newErrnoErrT[E](additionalInfo = additionalInfo)
204+
205+
proc raiseErrno*(additionalInfo: string) =
206+
raiseErrnoT[OSError](additionalInfo)
207+
208+
209+
proc raiseErrnoWithPath*[T](p: PathLike[T]; errno = getErrno()) =
199210
## raises OSError or its SubError.
200211
## refer to errno even under Windows.
201212
let errCode = errno.OSErrorCode
202213
template rErr(exc) =
203-
p.raiseExcWithPath(exc, errCode)
214+
p.raiseExcWithPath(exc, errCode, errnoMsgOSErr)
204215
errMap errCode, rErr, errnoMsgOSErr
205216
206217
when InJs:
@@ -210,25 +221,22 @@ when InJs:
210221
raise newException(exc, errMsg)
211222
template msgDiscardCode(_): string = errMsg
212223
errMap errCode, rErr, msgDiscardCode
213-
template catchJsErrAndRaise*(doSth) =
224+
225+
template catchJsErrAndDo(doSth; doErr) =
214226
var errMsg = ""
215227
let err = catchJsErrAsCode errMsg:
216228
doSth
217-
if err != 0:
218-
raiseErrnoWithMsg err, errMsg
219-
else:
220-
when not declared(errno):
221-
var errno{.importc, header: "<errno.h>".}: cint
222-
proc newErrnoErrT[E: OSError](additionalInfo = ""): owned(ref E) =
223-
newErrnoErrT[E](errno, additionalInfo)
224-
225-
proc raiseErrnoT*[E: OSError](additionalInfo = "") =
226-
## may raise `E` only
227-
raise newErrnoErrT[E](additionalInfo)
228-
proc raiseErrno*(additionalInfo = "") =
229-
raiseErrnoT[OSError](additionalInfo)
230-
231-
proc raiseErrnoWithPath*[T](p: PathLike[T]) =
232-
raiseErrnoWithPath(p)
229+
if err != 0: doErr err, errMsg
230+
template catchJsErrAndRaise*(doSth) =
231+
bind catchJsErrAndDo, raiseErrnoWithMsg
232+
template doErr(err, errMsg) =
233+
raiseErrnoWithMsg err, errMsg
234+
catchJsErrAndDo doSth, doErr
235+
236+
template catchJsErrAndSetErrno*(doSth) =
237+
bind catchJsErrAndDo, setErrnoRaw
238+
template doErr(err, errMsg) =
239+
setErrnoRaw err
240+
catchJsErrAndDo doSth, doErr
233241
234242

0 commit comments

Comments
 (0)