Skip to content

Commit

Permalink
fix BareExcept
Browse files Browse the repository at this point in the history
  • Loading branch information
bung87 committed May 3, 2023
1 parent 3842664 commit 468ec0a
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 25 deletions.
4 changes: 2 additions & 2 deletions src/scorper/http/exts/resumable.nim
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,13 @@ proc handleResumableUpload*(req: Request; resumableKeys = newResumableKeys()): F
s.flush
try:
s.close
except:
except CatchableError:
discard
inc j
file.flush
try:
file.close
except:
except CatchableError:
discard
let filepath = tmpDir / resumable.identifier
resumable.savePath = filepath
Expand Down
2 changes: 1 addition & 1 deletion src/scorper/http/httpbasicauth.nim
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ proc basicAuth*(request: Request, validator: HttpBasicAuthValidator): Future[boo
let s2 = authorization.find("Basic")
let decoded = base64.decode(authorization[s2+6 .. ^1])
up = decoded.split(":", 1)
except:
except CatchableError:
await request.respError(Http400, "Authorization header not valid")
return false
success = await validator.validate(up[0], up[1])
Expand Down
6 changes: 3 additions & 3 deletions src/scorper/http/streamclient.nim
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const defUserAgent* = "Nim httpclient/" & NimVersion
proc log(client: AsyncHttpClient, level: Level, args: varargs[string]) =
try:
client.log level, args
except:
except CatchableError:
discard

proc newProxy*(url: string, auth = ""): Proxy =
Expand Down Expand Up @@ -237,7 +237,7 @@ proc parseChunks(client: AsyncHttpClient): Future[void]
if chunkSizeStr == "":
try:
client.log lvlError, "Server terminated connection prematurely"
except:
except CatchableError:
discard
while i < chunkSizeStr.len:
case chunkSizeStr[i]
Expand Down Expand Up @@ -356,7 +356,7 @@ proc parseResponse*(client: AsyncHttpClient,
linei = 0
try:
line = await client.reader.readLine()
except:
except CatchableError:
line = ""
await sleepAsync(0)
continue
Expand Down
34 changes: 16 additions & 18 deletions src/scorper/http/streamserver.nim
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ macro acceptMime*(req: ImpRequest, ext: untyped, headers: HttpHeaders, body: unt
try:
{.cast(gcsafe).}:
r = req.server.privAccpetParser.match(accept, mimes)
except Exception as err:
except CatchableError as err:
await req.respError(Http500, err.msg, headers)
return
var ext {.inject.}: string
Expand All @@ -150,7 +150,7 @@ template devLog(req: ImpRequest, content: untyped) =
when not defined(release):
try:
req.server.logSub.next(`content`)
except:
except CatchableError:
discard

template handleCompress(needCompress: bool; content: string; ctn: var string; length: var int) =
Expand Down Expand Up @@ -292,10 +292,10 @@ proc writeFile(req: ImpRequest, fname: string, size: int): Future[void] {.async.
var file: File
try:
file = open(fname)
except Exception as e:
except CatchableError as e:
try:
req.server.logSub.next(e.msg)
except Exception:
except CatchableError:
discard
return
when defined(windows):
Expand Down Expand Up @@ -337,10 +337,10 @@ proc writePartialFile(req: ImpRequest, fname: string, ranges: seq[tuple[starts:
if not req.server.isSecurity:
try:
file = open(fname)
except Exception as e:
except CatchableError as e:
try:
req.server.logSub.next(e.msg)
except Exception:
except Defect, CatchableError:
await req.respError(Http500)
return
when defined(windows):
Expand All @@ -361,7 +361,7 @@ proc writePartialFile(req: ImpRequest, fname: string, ranges: seq[tuple[starts:
if req.server.isSecurity:
try:
writeFileStream(req, fname, offset, size)
except Exception:
except Defect, CatchableError:
await req.respError(Http500)
return
else:
Expand All @@ -371,7 +371,7 @@ proc writePartialFile(req: ImpRequest, fname: string, ranges: seq[tuple[starts:
else:
try:
writeFileStream(req, fname, offset, size)
except Exception:
except Defect, CatchableError:
await req.respError(Http500)
return
await req.writer.write(CRLF & boundary & "--")
Expand All @@ -386,7 +386,7 @@ proc fileGuard(req: ImpRequest, filepath: string): Future[Option[FileInfo]] {.as
var info: FileInfo
try:
info = getFileInfo(filepath)
except:
except CatchableError:
return none(FileInfo)
if fpOthersRead notin info.permissions:
await req.respError(Http403)
Expand All @@ -395,7 +395,7 @@ proc fileGuard(req: ImpRequest, filepath: string): Future[Option[FileInfo]] {.as
var ifModifiedSince: Time
try:
ifModifiedSince = parseTime(req.headers["If-Modified-Since"], HttpDateFormat, utc())
except:
except CatchableError:
await req.respError(Http400)
return none(FileInfo)
if info.lastWriteTime == ifModifiedSince:
Expand All @@ -405,7 +405,7 @@ proc fileGuard(req: ImpRequest, filepath: string): Future[Option[FileInfo]] {.as
var ifUnModifiedSince: Time
try:
ifUnModifiedSince = parseTime(req.headers["If-Unmodified-Since"], HttpDateFormat, utc())
except:
except CatchableError:
await req.respError(Http400)
return none(FileInfo)
if info.lastWriteTime > ifUnModifiedSince:
Expand Down Expand Up @@ -523,7 +523,7 @@ proc serveStatic*(req: Request) {.async.} =
var relPath: string
try:
relPath = decodeUrlComponent(req.url.path.relativePath(cast[ImpRequest](req).prefix))
except:
except CatchableError:
discard
if not hasSuffix(relPath):
relPath = relPath / "index.html"
Expand Down Expand Up @@ -564,8 +564,6 @@ proc json*(req: ImpRequest): Future[JsonNode] {.async.} =
result = parseJson(str)
except CatchableError as e:
raise newHttpError(Http400, e.msg)
except Exception as e:
raise newHttpError(Http400, e.msg)
req.parsedJson = some(result)
req.parsed = true

Expand Down Expand Up @@ -622,7 +620,7 @@ proc form*(req: ImpRequest): Future[Form] {.async.} =
else:
try:
req.server.logSub.next("form parse error: " & $parser.state)
except:
except CatchableError:
discard
else:
discard
Expand Down Expand Up @@ -667,7 +665,7 @@ template tryHandle(body: untyped, keep: var bool) =
except HttpError as err:
if not req.responded:
await req.defaultErrorHandle(err)
except:
except CatchableError:
if not req.responded:
let err = getCurrentException()
await req.defaultErrorHandle(err)
Expand Down Expand Up @@ -895,11 +893,11 @@ template initScorper(server: Scorper) =
var obs = newObservable[string]()
try:
discard subscribe[string](obs, server.logSub)
except:
except CatchableError:
discard
try:
server.logSub.next("Scorper serve at http" & (if isSecurity: "s" else: "") & "://" & $server.local)
except:
except CatchableError:
discard

proc serve*(address: string,
Expand Down
2 changes: 1 addition & 1 deletion tests/tjson.nim
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ suite "test json":
var s: string
try:
toUgly(s, parseJson("""{ "name": "Nim", "age": 12 }"""))
except:
except CatchableError:
discard
doAssert(body == s)

Expand Down

0 comments on commit 468ec0a

Please sign in to comment.