Error Handling
Making all() and one() requests
There is two exceptions that you can catch when using the all()
and one()
:
NotFoundException
Will be throw if the target query response don't exists, not found ( when a 400 status code is got ) Example:
try {
List<Products> categoryProducts = await YouCan.instance.categories.category("WRONG CATEGORY ID").products.all();
} on NotFoundException catch (e) {
print(e.message);
} catch (e){
print(e);
}
In this example, we've put a wrong category id in the category()
method, this will lead directly to throw an NotFoundException
since there is category found with that id,and you can catch this easily with a try catch
like above.
ServiceNotAvailable
Will be thrown when the YouCan service is unavailable ( when a 500 status code is got )
try {
List<Products> categoryProducts = await YouCan.instance.products.all();
} on ServiceNotAvailable catch (e) {
print(e.message);
} catch (e){
print(e);
}
We don't own and we don't have any kind of access to the YouCan server, if the server goes down as example and you tried to make a request, it will fail and a ServiceNotAvailable
will be thrown, which you can catch like above.
You can use this snippet to make request and handle possible exceptions :
try {
final response = await YouCan.instance.products.all();
} on NotFoundException catch(e) {
/*...*/
} on ServiceNotAvailable catch(e) {
/*...*/
} catch(e) {
/*...*/
}
## Making request methods that requires inputs
On the other hands, when you call methods that takes arguments and behave to update, add something to your store such as the login()
, createCustomer()
, updateCustomerAccount()
..., there is some exceptions that you might want to aware of:
InvalidDataException
Will be thrown when a field value is invalid to be used to complete the request, as example if wa wanted to update the customer account, we will use the updateCustomerAccount()
, right? :
try {
List<Products> categoryProducts = await YouCan.instance.customers.updateCustomerAccount(
userTokeb: "CORRECT USER TOKEN",
newUserData: NewUserData(
email: "Text@", // not valid email
),
);
} on InvalidDataException catch (e) {
print(e.message);
print(e.reason); //
} catch (e){
print(e);
}
Now with this, when the code is executed, the InvalidDataException
will be thrown because the email we put is invalid, you can catch it and get the reason exactly why it's thrown to fix it.