Error handling is hard, harder than you think πŸ€”

Let's just see a simple example πŸ’πŸΌβ€β™‚οΈ

In how many ways can this #dart code below fail?

The answer will surprise you πŸ‘‡πŸ§΅ Image
Note: fail can mean different things ☝️

Here we mean anything that will crash the app (if not handled)

Errors or Exceptions πŸ‘‡

And the answer is... πŸ”Ÿ

Yes, 10 ways 🀯

Basically every function hides a potential issue

What? Well, let's see them all πŸ‘‡
1️⃣ Uri.https

Yes, this can fail πŸ™„

Just take a look at its internal implementation: FormatException! Image
2️⃣ _httpClient.get

Well, that was easy πŸ˜…

Anything can fail in an http request, pay attention
3️⃣ statusCode != 200

Here we throw manually

Remember to check the status code, it may not always be obvious ☝️
4️⃣ jsonDecode

This needs the correct input format, otherwise πŸ’₯ Image
5️⃣ as Map

Yes, 'as' can throw as well 😱

Check the thread below for more about this πŸ‘‡

6️⃣ containsKey('results')

Here again we throw manually

We cannot do otherwise, we need the results πŸ’πŸΌβ€β™‚οΈ
7️⃣ as List

Again, 'as' is insidious 🐍

Check the docs for more details πŸ“‘

dart.dev/guides/languag…
8️⃣ isEmpty

If not results, then no data, then we throw πŸ’πŸΌβ€β™‚οΈ
9️⃣ as Map<String, dynamic>

'as' πŸ™„

πŸ”Ÿ Location.fromJson

fromJson hides an 'as' casting πŸ‘»

Well, if the data is invalid we cannot avoid throwing an Exception Image
Being aware of all the possible issues is the first step

Then it's all about making sure the app will not fail

And that's a whole new challenge πŸ™ƒ
There are also other ways to handle errors in your app πŸ”œ

Follow @SandroMaglione (me πŸ‘‹) for more about #dart and #flutter

β€’ β€’ β€’

Missing some Tweet in this thread? You can try to force a refresh
γ€€

Keep Current with Sandro Maglione πŸ’»

Sandro Maglione πŸ’» Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @SandroMaglione

Oct 17
What if your #flutter app fails? Should you throw? Error? Exception?

#dart gives you both Error and Exception 🎯

But how do they work? Which one should you choose? πŸ€”

Here is the answer πŸ‘‡πŸ§΅
Error & Exception ‼️

#dart has both of them, but they are not the same πŸ’πŸΌβ€β™‚οΈ

πŸ‘‰ Exceptions: conditions that you can plan ahead for *and catch*

πŸ‘‰ Errors: conditions that *you don’t expect*
Exceptions should be handled in your code πŸ’»

You (developer πŸ’πŸΌβ€β™‚οΈ) know that they can happen, and so you plan accordingly

πŸ‘‰ *It is intended to be caught*

πŸ‘‰ It should contain useful data fields

πŸ‘‰ Should not crash the app
Read 12 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(