Skip to content

Commit 3bcd1aa

Browse files
authored
Let Mongo.Repo errors flow like with Mongo functions (#219)
When `Mongo` returns a `Mongo.Error`, we get match errors inside `Enum`, which are hard to read and sometimes misleading. This patch changes signatures to let the errors flow. This is argably a breaking change, but errors will happen anyway in problematic scenario. As alternative, `Mongo.Error` could be `raise`d and signtures are kept the same. At least errors will be more clear to library users.
1 parent 82123b2 commit 3bcd1aa

File tree

1 file changed

+25
-18
lines changed

1 file changed

+25
-18
lines changed

lib/mongo/repo.ex

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -176,52 +176,59 @@ defmodule Mongo.Repo do
176176
def all(module, filter \\ %{}, opts \\ []) do
177177
collection = module.__collection__(:collection)
178178

179-
@topology
180-
|> Mongo.find(collection, filter, opts)
181-
|> Enum.map(&module.load/1)
179+
case Mongo.find(@topology, collection, filter, opts) do
180+
{:error, _reason} = error -> error
181+
cursor -> Enum.map(cursor, &module.load/1)
182+
end
182183
end
183184

184185
def stream(module, filter \\ %{}, opts \\ []) do
185186
collection = module.__collection__(:collection)
186187

187-
@topology
188-
|> Mongo.find(collection, module.dump_part(filter), opts)
189-
|> Stream.map(&module.load/1)
188+
case Mongo.find(@topology, collection, module.dump_part(filter), opts) do
189+
{:error, _reason} = error -> error
190+
cursor -> Stream.map(cursor, &module.load/1)
191+
end
190192
end
191193

192194
def aggregate(module, pipeline, opts \\ []) do
193195
collection = module.__collection__(:collection)
194196

195-
@topology
196-
|> Mongo.aggregate(collection, pipeline, opts)
197-
|> Enum.map(&module.load/1)
197+
case Mongo.aggregate(@topology, collection, pipeline, opts) do
198+
{:error, _reason} = error -> error
199+
cursor -> Enum.map(cursor, &module.load/1)
200+
end
198201
end
199202

200203
def get(module, id, opts \\ []) do
201204
collection = module.__collection__(:collection)
202205

203-
@topology
204-
|> Mongo.find_one(collection, %{_id: id}, opts)
205-
|> module.load()
206+
case Mongo.find_one(@topology, collection, %{_id: id}, opts) do
207+
{:error, _reason} = error -> error
208+
value -> module.load(value)
209+
end
206210
end
207211

208212
def get_by(module, filter \\ %{}, opts \\ []) do
209213
collection = module.__collection__(:collection)
210214

211-
@topology
212-
|> Mongo.find_one(collection, module.dump_part(filter), opts)
213-
|> module.load()
215+
case Mongo.find_one(@topology, collection, module.dump_part(filter), opts) do
216+
{:error, _reason} = error -> error
217+
value -> module.load(value)
218+
end
214219
end
215220

216221
def fetch(module, id, opts \\ []) do
217222
case get(module, id, opts) do
223+
{:error, _reason} = error -> error
218224
nil -> {:error, :not_found}
219225
doc -> {:ok, doc}
220226
end
221227
end
222228

223229
def fetch_by(module, filter \\ %{}, opts \\ []) do
224230
case get_by(module, module.dump_part(filter), opts) do
231+
{:error, _reason} = error -> error
225232
nil -> {:error, :not_found}
226233
doc -> {:ok, doc}
227234
end
@@ -297,7 +304,7 @@ defmodule Mongo.Repo do
297304
MyApp.Repo.all(Post, %{title: title}, batch_size: 2)
298305
"""
299306
@callback all(module :: module(), filter :: BSON.document(), opts :: Keyword.t()) ::
300-
list(Mongo.Collection.t())
307+
list(Mongo.Collection.t()) | {:error, any()}
301308

302309
@doc """
303310
Selects documents for the collection defined in the given module and returns a stream of collection
@@ -311,7 +318,7 @@ defmodule Mongo.Repo do
311318
MyApp.Repo.stream(Post, %{title: title}, batch_size: 2)
312319
"""
313320
@callback stream(module :: module(), filter :: BSON.document(), opts :: Keyword.t()) ::
314-
Enumerable.t()
321+
Enumerable.t() | {:error, any()}
315322

316323
@doc """
317324
Performs aggregation operation using the aggregation pipeline on the given collection module and returns
@@ -328,7 +335,7 @@ defmodule Mongo.Repo do
328335
])
329336
"""
330337
@callback aggregate(module :: module(), pipeline :: BSON.document(), opts :: Keyword.t()) ::
331-
list(Mongo.Collection.t())
338+
list(Mongo.Collection.t()) | {:error, any()}
332339

333340
@doc """
334341
Returns the count of documents in the given collection module for the given filter.

0 commit comments

Comments
 (0)