1
Future<List<Business>> getBusiness(Session session, {int? id}) async {
    return await Business.find(
      session,
      where: (t) => id != null ? t.id.equals(id) : Constant(true),
    );
  }

How to return the error model if any error happens, like if the id doesn't exist or something

1 Answers1

0

Since the find method returns a List it would be empty, you can check if the list is empty and then throw a Serialsable exception.

However, in your query, it looks like you are only interested in a specific row. There is a special function for this use case that you can use instead, findById that takes the session object and an id.

Future<Business> getBusiness(Session session, int id) async {
  final business = await Business.findById(
    session,
    id
  );

  if (business == null) {
    throw NotFound();
  }

  return business;
}

In your protocol folder you would have something like this:

## not_found.yaml

exception: NotFound
fields:
  message: String

On the client side:

try {
  final result = await client.business.getBusiness(id);
} on (NotFound) catch (error) {
  // Handle not found.
} catch (error) {
  // Unexpected error.
}
Isak dl
  • 448
  • 3
  • 13