🌎
165 | 166 | 167 | ``` 168 | ```/home/ubuntu/404.html 169 | 170 | 171 | 172 | 173 |🤷♀️
178 | 179 | 180 | ``` 181 |4 | 5 | I think throwing exceptions is nice, I like that an exception breaks control flow and I like exception propogation. The only thing I don't like catching exceptions. 6 | 7 | This mostly happens at the most "user facing" part of the code like an api endpoint or a UI component, the outer most function call. So catching an exception needs to notify the user that something went wrong, log the error for debugging, and stop the currently execution flow. 8 | 9 | ## Guard ✅ 10 | 11 | Guarding allows you to handle your errors early and return from the function early, making them more readable and easier to reason about. 12 | 13 | ```ts 14 | const [ networkError, result ] = await mightFail(fetch("/posts")); 15 | // guard against a network error 16 | if (networkError) { 17 | return; 18 | } 19 | // guard against an error response from the server 20 | if (!result.ok) { 21 | return; 22 | } 23 | const [ convertToJSONError, posts ] = await mightFail( 24 | result.json() 25 | ); 26 | // guard against an error converting the response to JSON 27 | if (convertToJSONError) { 28 | return; 29 | } 30 | 31 | // success case, unnested and at the bottom of the function 32 | posts.map((post) => console.log(post.title)); 33 | ``` 34 | 35 | The success case is now the only code that is not nested in an `if` statement. It's also at the very bottom of the function making it easy to find. 36 | 37 | ## Everything in One Try/Catch Block ❌ 38 | 39 | ```ts 40 | try { 41 | const response = await fetch("/posts"); 42 | 43 | if (!response.ok) { 44 | // handle an error response from server 45 | return; 46 | } 47 | const posts = await response.json(); 48 | 49 | posts.map((post) => console.log(post.title)); 50 | } catch (error) { 51 | // handle any errors, not sure which one though 🤷♀️ 52 | } 53 | ``` 54 | 55 | This is bad because: 56 | 57 | - Error handling happens in multiple places in the function. 58 | - The catch block will catch **any** and **all** errors which makes it difficult to handle different errors differently. 59 | - All the success case code will happen inside of the try block 60 | 61 | ## Multiple Try/Catch Blocks ❌ 62 | 63 | ```ts 64 | let response: Response; 65 | try { 66 | response = await fetch("/posts"); 67 | } catch (error) { 68 | // guard against a network error 69 | return; 70 | } 71 | if (!response.ok) { 72 | // guard against an error response from server 73 | return; 74 | } 75 | 76 | let posts: Post[]; 77 | try { 78 | posts = await response.json(); 79 | } catch (error) { 80 | // guard against an error converting the response to JSON 81 | return; 82 | } 83 | 84 | posts.map((post) => console.log(post.title)); 85 | ``` 86 | 87 | Declaring the variable ahead of time is a little weird and it makes infering the type of the variable a little more difficult. Also, try catch finally blocks can be confusing. 88 | 89 | ## `try` `catch` `finally` can be confusing ❌ 90 | 91 | ```ts 92 | function something() { 93 | try { 94 | throw new Error("something went wrong"); 95 | } catch(error) { 96 | console.log("error happened") 97 | return "error return" 98 | } finally { 99 | console.log("finally happened") 100 | return "finally return" 101 | } 102 | return "something return" 103 | } 104 | console.log(something()) 105 | ``` 106 | 107 | Can every single dev in your team understand what the above code will print out? 108 | -------------------------------------------------------------------------------- /docs/content/home.mdx: -------------------------------------------------------------------------------- 1 | 2 | [](https://github.com/might-fail/ts) 3 | 4 | A TypeScript library for handling async and sync errors without `try` and `catch` blocks. Inspired by other languages that utilize `Result` or `Either` types for safer error handling. 5 | 6 | This works for **sync** and **async** code, and you can choose the error handling style that you like. 7 | 8 | ## Other languages 9 | 10 | * [Swift](https://swift.mightfail.dev/documentation/MightFail) 11 | * [PHP](https://github.com/might-fail/php) 12 | 13 | ## Install 14 | 15 |try catch blocks are just confusing pic.twitter.com/IFaLOtV2nr
— Sam Meech Ward (@Meech_Ward) October 13, 2024