@@ -14,6 +14,7 @@ when InJs:
14
14
15
15
import ../ io_abc
16
16
import ../ private/ backendMark
17
+ import ../ Lib/ errno_impl/ errnoUtils
17
18
export OSErrorCode
18
19
type
19
20
FileNotFoundError* = object of OSError
62
63
func newOSErrorWithMsg(err: OSErrorCode, msg: string ): owned (ref OSError) =
63
64
(ref OSError)(errorCode: err.int32 , msg: msg)
64
65
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))
67
69
68
70
func raiseExcWithPath* (fp: PathLike, exc: typedesc , err: OSErrorCode, additionalInfo: string ) =
69
71
var msg = fp.osErrorMsgWithPath(err)
@@ -94,7 +96,7 @@ when defined(nimscript):
94
96
ErrExist = NON_ERR_CODE
95
97
ErrNoent = NON_ERR_CODE
96
98
ErrIsdir = NON_ERR_CODE
97
- elif InJs: discard
99
+ elif InJs: discard # those are defined in ./jsoserr
98
100
else :
99
101
100
102
when defined(windows):
@@ -169,38 +171,47 @@ template tryOsOp*(p: PathLike, body) =
169
171
170
172
when InJs:
171
173
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)
173
175
elif not weirdTarget:
174
176
proc c_strerror(code: cint ): cstring {.importc: "strerror", header: "<string .h>".}
175
177
176
178
func errnoMsgOSErr(errnoCode: OSErrorCode): string = $c_strerror(errnoCode.cint )
177
179
178
- func errnoMsg*(errnoCode : cint ): string = $c_strerror(errnoCode )
180
+ func errnoMsg*(errno : cint ): string = $c_strerror(errno )
179
181
180
182
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 ))
183
185
if additionalInfo.len > 0:
184
186
if result .msg.len > 0 and result .msg[^ 1] != '\n ' : result .msg.add '\n '
185
187
result .msg.add " Additional info: "
186
188
result .msg.add additionalInfo
187
189
# don't add trailing `.` etc, which negatively impacts "jump to file" in IDEs.
188
190
if result .msg == " " :
189
191
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)
192
194
193
-
194
- proc raiseErrno*(errno: cint ; additionalInfo = "") =
195
+ proc raiseErrno*(errno=getErrno(); additionalInfo = "") =
195
196
## may raise OSError only
196
197
raise newErrnoErr(errno, additionalInfo)
197
198
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()) =
199
210
## raises OSError or its SubError.
200
211
## refer to errno even under Windows.
201
212
let errCode = errno.OSErrorCode
202
213
template rErr(exc) =
203
- p.raiseExcWithPath(exc, errCode)
214
+ p.raiseExcWithPath(exc, errCode, errnoMsgOSErr )
204
215
errMap errCode, rErr, errnoMsgOSErr
205
216
206
217
when InJs:
@@ -210,25 +221,22 @@ when InJs:
210
221
raise newException(exc, errMsg)
211
222
template msgDiscardCode(_): string = errMsg
212
223
errMap errCode, rErr, msgDiscardCode
213
- template catchJsErrAndRaise*(doSth) =
224
+
225
+ template catchJsErrAndDo(doSth; doErr) =
214
226
var errMsg = ""
215
227
let err = catchJsErrAsCode errMsg:
216
228
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
233
241
234
242
0 commit comments