From 468ec0ace291e510e2af0f9e7c8d1f7ce28f4ab5 Mon Sep 17 00:00:00 2001 From: Bung Date: Wed, 3 May 2023 21:10:43 +0800 Subject: [PATCH] fix BareExcept --- src/scorper/http/exts/resumable.nim | 4 ++-- src/scorper/http/httpbasicauth.nim | 2 +- src/scorper/http/streamclient.nim | 6 ++--- src/scorper/http/streamserver.nim | 34 ++++++++++++++--------------- tests/tjson.nim | 2 +- 5 files changed, 23 insertions(+), 25 deletions(-) diff --git a/src/scorper/http/exts/resumable.nim b/src/scorper/http/exts/resumable.nim index e9ccaf4..ab65b80 100644 --- a/src/scorper/http/exts/resumable.nim +++ b/src/scorper/http/exts/resumable.nim @@ -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 diff --git a/src/scorper/http/httpbasicauth.nim b/src/scorper/http/httpbasicauth.nim index 1ecf66c..69beb51 100644 --- a/src/scorper/http/httpbasicauth.nim +++ b/src/scorper/http/httpbasicauth.nim @@ -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]) diff --git a/src/scorper/http/streamclient.nim b/src/scorper/http/streamclient.nim index 365a9f1..b62a4f4 100644 --- a/src/scorper/http/streamclient.nim +++ b/src/scorper/http/streamclient.nim @@ -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 = @@ -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] @@ -356,7 +356,7 @@ proc parseResponse*(client: AsyncHttpClient, linei = 0 try: line = await client.reader.readLine() - except: + except CatchableError: line = "" await sleepAsync(0) continue diff --git a/src/scorper/http/streamserver.nim b/src/scorper/http/streamserver.nim index 2674f31..f3b670a 100644 --- a/src/scorper/http/streamserver.nim +++ b/src/scorper/http/streamserver.nim @@ -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 @@ -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) = @@ -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): @@ -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): @@ -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: @@ -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 & "--") @@ -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) @@ -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: @@ -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: @@ -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" @@ -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 @@ -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 @@ -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) @@ -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, diff --git a/tests/tjson.nim b/tests/tjson.nim index 7bbadc9..88c66db 100644 --- a/tests/tjson.nim +++ b/tests/tjson.nim @@ -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)