Skip to content

Commit 2b0b74c

Browse files
fix: not found error in stream queries (#27)
1 parent c9353d6 commit 2b0b74c

File tree

3 files changed

+38
-13
lines changed

3 files changed

+38
-13
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,4 @@ project/metals.sbt
9090
coursier
9191

9292
*.semanticdb
93+
nohup.*

arangodb-http/src/it/scala/io/funkode/arangodb/http/ArangoJsonIT.scala

+20-6
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,15 @@ trait ArangoExamples:
7373
val geographyCollection2 = CollectionName("geography2")
7474
val countries = CollectionName("countries")
7575
val continents = CollectionName("continents")
76+
val continents2 = CollectionName("continents2")
7677
val oceans = CollectionName("oceans")
7778
val oceans2 = CollectionName("oceans2")
7879
val graphEdgeDefinitions = List(GraphEdgeDefinition(allies, List(countries), List(countries)))
7980
val graphEdgeDefinitionsGeography = List(
8081
GraphEdgeDefinition(geographyCollection, List(continents), List(continents))
8182
)
8283
val graphEdgeDefinitionsGeography2 = List(
83-
GraphEdgeDefinition(geographyCollection2, List(continents), List(continents))
84+
GraphEdgeDefinition(geographyCollection2, List(continents2), List(continents2))
8485
)
8586
val es = DocumentHandle(countries, DocumentKey("ES"))
8687
val fr = DocumentHandle(countries, DocumentKey("FR"))
@@ -205,17 +206,30 @@ object ArangoJsonIT extends ZIOSpecDefault with ArangoExamples:
205206
.read[Pet]()
206207
.flip
207208
.debug("test collection doesn't exist")
208-
yield assertTrue(error.code == 404L)
209+
yield assertTrue(
210+
error == ArangoError(404L, true, "collection or view not found: collectionDoesntExist", 1203L)
211+
)
209212
},
210213
test("Manage document doesn't exist when fetching document") {
211214
for
212-
invalidCollection <- ArangoClientJson.collection(CollectionName("countries"))
213-
error <- invalidCollection
215+
countriesCollection <- ArangoClientJson.collection(CollectionName("countries"))
216+
error <- countriesCollection
214217
.document(DocumentKey("turtle"))
215218
.read[Pet]()
216219
.flip
217220
.debug("test document doesn't exist")
218-
yield assertTrue(error.code == 404L)
221+
yield assertTrue(error == ArangoError(404L, true, "document not found", 1202L))
222+
},
223+
test("Manage document doesn't exist when fetching raw documents") {
224+
for
225+
countriesCollection <- ArangoClientJson.collection(CollectionName("countries"))
226+
error <- countriesCollection
227+
.document(DocumentKey("turtle"))
228+
.readRaw()
229+
.flatMap(_.via(ZPipeline.utf8Decode).run(ZSink.collectAll))
230+
.flip
231+
.debug("test document raw doesn't exist")
232+
yield assertTrue(error == ArangoError(404L, true, "document not found", 1202L))
219233
},
220234
test("Query documents with cursor") {
221235
for
@@ -280,7 +294,7 @@ object ArangoJsonIT extends ZIOSpecDefault with ArangoExamples:
280294
_ <- graph.removeVertexCollection(oceans2)
281295
collectionsAfter <- graph.vertexCollections
282296
yield assertTrue(collectionsBefore != collectionsAfter) && assertTrue(
283-
collectionsAfter == List(continents)
297+
collectionsAfter == List(continents2)
284298
)
285299
},
286300
test("Save and retrieve document from byte array stream") {

arangodb-http/src/main/scala/io/funkode/arangodb/http/ArangoClientHttp.scala

+17-7
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,10 @@ class ArangoClientHttp[Encoder[_], Decoder[_]](
8181
yield response
8282

8383
def getRaw(header: ArangoMessage.Header): AIO[ArangoStreamRaw] =
84-
for response <- httpClient.request(header.emptyRequest(BaseUrl, headers)).handleErrors
85-
yield response.body.asStream.handleStreamErrors
84+
for
85+
response <- httpClient.request(header.emptyRequest(BaseUrl, headers)).handleErrors
86+
parsedResponse <- parseResponseBodyRaw(response)
87+
yield parsedResponse
8688

8789
def get[O: Decoder](header: ArangoMessage.Header): AIO[ArangoMessage[O]] =
8890
for
@@ -95,8 +97,10 @@ class ArangoClientHttp[Encoder[_], Decoder[_]](
9597
): AIO[ArangoStreamRaw] =
9698
val header = message.header.emptyRequest(BaseUrl, headers)
9799
val request: Request = header.copy(body = Body.fromStream(message.body))
98-
for response <- httpClient.request(request).handleErrors
99-
yield response.body.asStream.handleStreamErrors
100+
for
101+
response <- httpClient.request(request).handleErrors
102+
parsedResponse <- parseResponseBodyRaw(response)
103+
yield parsedResponse
100104

101105
def command[I: Encoder, O: Decoder](message: ArangoMessage[I]): AIO[ArangoMessage[O]] =
102106
val header = message.header.emptyRequest(BaseUrl, headers)
@@ -124,6 +128,12 @@ class ArangoClientHttp[Encoder[_], Decoder[_]](
124128
new ArangoClientHttp[Encoder, Decoder](newConfig, httpClient, token)
125129

126130
private def parseResponseBody[O: Decoder](response: Response): AIO[O] =
131+
parseResponseBodyInternal(response)(httpDecoder.decode[O])
132+
133+
private def parseResponseBodyRaw(response: Response): AIO[ArangoStreamRaw] =
134+
parseResponseBodyInternal(response)(body => ZIO.succeed(body.asStream.handleStreamErrors))
135+
136+
private def parseResponseBodyInternal[O](response: Response)(onSucces: Body => AIO[O]): AIO[O] =
127137
if response.status.isError
128138
then
129139
httpDecoder
@@ -149,7 +159,7 @@ class ArangoClientHttp[Encoder[_], Decoder[_]](
149159
},
150160
arangoError => ZIO.fail(arangoError)
151161
)
152-
else httpDecoder.decode[O](response.body)
162+
else onSucces(response.body)
153163

154164
def currentDatabase: DatabaseName = (config.database)
155165

@@ -273,8 +283,8 @@ object extensions:
273283
extension [A](call: IO[Throwable, A])
274284
def handleErrors: IO[ArangoError, A] = call.catchAll(e => ZIO.fail(throwableToArangoError(e)))
275285

276-
extension [A](stream: Stream[Throwable, A])
277-
def handleStreamErrors: ArangoStream[A] = stream.catchAll(e => ZStream.fail(throwableToArangoError(e)))
286+
extension (stream: Stream[Throwable, Byte])
287+
def handleStreamErrors: ArangoStreamRaw = stream.catchAll(e => ZStream.fail(throwableToArangoError(e)))
278288

279289
object ArangoClientSchema:
280290

0 commit comments

Comments
 (0)