Skip to content

Commit

Permalink
Handles LPStreamError in chunker (#947)
Browse files Browse the repository at this point in the history
* Handles LPStreamError in chunker

* Adds test for lpstream exception

* Adds tests for other stream exceptions. Cleanup.
  • Loading branch information
benbierens authored Oct 10, 2024
1 parent 1fe3abf commit 3699601
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
7 changes: 5 additions & 2 deletions codex/chunker.nim
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,11 @@ proc new*(
trace "LPStreamChunker stream Eof", exc = exc.msg
except CancelledError as error:
raise error
except LPStreamError as error:
error "LPStream error", err = error.msg
raise error
except CatchableError as exc:
trace "CatchableError exception", exc = exc.msg
error "CatchableError exception", exc = exc.msg
raise newException(Defect, exc.msg)

return res
Expand Down Expand Up @@ -127,7 +130,7 @@ proc new*(
except CancelledError as error:
raise error
except CatchableError as exc:
trace "CatchableError exception", exc = exc.msg
error "CatchableError exception", exc = exc.msg
raise newException(Defect, exc.msg)

return total
Expand Down
2 changes: 1 addition & 1 deletion codex/rest/api.nim
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ proc initDataApi(node: CodexNodeRef, repoStore: RepoStore, router: var RestRoute
try:
without cid =? (
await node.store(AsyncStreamWrapper.new(reader = AsyncStreamReader(reader)))), error:
trace "Error uploading file", exc = error.msg
error "Error uploading file", exc = error.msg
return RestApiResponse.error(Http500, error.msg)

codex_api_uploads.inc()
Expand Down
39 changes: 38 additions & 1 deletion tests/codex/testchunking.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import pkg/stew/byteutils
import pkg/codex/chunker
import pkg/codex/logutils
Expand All @@ -7,6 +6,17 @@ import pkg/chronos
import ../asynctest
import ./helpers

type
CrashingStreamWrapper* = ref object of LPStream
toRaise*: ref CatchableError

method readOnce*(
self: CrashingStreamWrapper,
pbytes: pointer,
nbytes: int
): Future[int] {.async.} =
raise self.toRaise

asyncchecksuite "Chunking":
test "should return proper size chunks":
var offset = 0
Expand Down Expand Up @@ -78,3 +88,30 @@ asyncchecksuite "Chunking":
string.fromBytes(data) == readFile(path)
fileChunker.offset == data.len

proc raiseStreamException(exc: ref CatchableError) {.async.} =
let stream = CrashingStreamWrapper.new()
let chunker = LPStreamChunker.new(
stream = stream,
chunkSize = 2'nb)

stream.toRaise = exc
discard (await chunker.getBytes())

test "stream should forward LPStreamError":
expect LPStreamError:
await raiseStreamException(newException(LPStreamError, "test error"))

test "stream should catch LPStreamEOFError":
await raiseStreamException(newException(LPStreamEOFError, "test error"))

test "stream should forward CancelledError":
expect CancelledError:
await raiseStreamException(newException(CancelledError, "test error"))

test "stream should forward LPStreamError":
expect LPStreamError:
await raiseStreamException(newException(LPStreamError, "test error"))

test "stream should convert other exceptions to defect":
expect Defect:
await raiseStreamException(newException(CatchableError, "test error"))

0 comments on commit 3699601

Please sign in to comment.