I'm Ditching Try/Catch for Good!

แชร์
ฝัง
  • เผยแพร่เมื่อ 21 ธ.ค. 2024

ความคิดเห็น • 696

  • @567saturn
    @567saturn 2 หลายเดือนก่อน +946

    How come you ditch everything every week?

    • @svens3722
      @svens3722 2 หลายเดือนก่อน +171

      hes dutch

    • @koruko634
      @koruko634 2 หลายเดือนก่อน

      @@svens3722😂

    • @theankushgautam
      @theankushgautam 2 หลายเดือนก่อน

      ​@@svens3722 😂

    • @Saint_Crocodile
      @Saint_Crocodile 2 หลายเดือนก่อน +154

      Well he has to run a youtube channel 😅

    • @PraiseYeezus
      @PraiseYeezus 2 หลายเดือนก่อน +106

      This was also my reaction when I discovered there was this weird concept called "learning" that lets you find new strategies that you like better than your old ones.

  • @moonwhisperer4804
    @moonwhisperer4804 2 หลายเดือนก่อน +281

    I don’t buy it… you can just put try catch on separate sections … just keep it simple

    • @caribouroadfarm
      @caribouroadfarm 2 หลายเดือนก่อน +18

      Not only that you can have multiple catch clauses for a try clause.

    • @genechristiansomoza4931
      @genechristiansomoza4931 2 หลายเดือนก่อน +10

      Usually the controller is the best place to put try catch

    • @Kriszzzful
      @Kriszzzful 2 หลายเดือนก่อน

      @@caribouroadfarm You can? I just checked MDN and didn't find it, can you post a link? Or do you mean multiple if/else in a single catch block?

    • @vagkalosynakis
      @vagkalosynakis 2 หลายเดือนก่อน +9

      No dude, you're supposed to work differently to how the rest of the industry and your team works, you don't get it! /s

    • @caribouroadfarm
      @caribouroadfarm 2 หลายเดือนก่อน

      @@Kriszzzful I don't know if you can post links here, but those things called Conditional catch clauses just google it

  • @notoavinarazafilalao7415
    @notoavinarazafilalao7415 2 หลายเดือนก่อน +18

    we don't talk much about Effect, this lib is impressive

    • @eprd313
      @eprd313 2 หลายเดือนก่อน +1

      What do you use it for mostly if I may ask?

    • @notoavinarazafilalao7415
      @notoavinarazafilalao7415 2 หลายเดือนก่อน

      @@eprd313 Honestly, I'm not fully using it in my codebase yet, but you can watch Lucas Barake's videos with some cool examples

    • @PeterSahanaya
      @PeterSahanaya วันที่ผ่านมา

      @@eprd313 I use it just for error handling on my Next.js app, it's cool i can catch specific error, i use it a lot on server actions

  • @clevermissfox
    @clevermissfox 2 หลายเดือนก่อน +286

    We’ve come full circle; ditching try/catch for .then().catch() 😆

    • @moligun83
      @moligun83 2 หลายเดือนก่อน +35

      That's what drove me crazy: async/await is syntax sugar that produces the exact same Promise. The error handling function could have easily been written as an async function with a try/catch block.

    • @dio56301
      @dio56301 2 หลายเดือนก่อน +3

      yeah, was thinking the same (that it could be rewritten to async try/catch) ... so what am I missing here?

    • @gohancomfejao
      @gohancomfejao 2 หลายเดือนก่อน +7

      Yeah it’s the exact same thing, but this creates reusable functionality. Otherwise you’d have to reimplement the same function for everything that handles promises.

    • @thescientisthorse
      @thescientisthorse หลายเดือนก่อน +11

      I get "callback hell" vibe

    • @WhiteVanyo
      @WhiteVanyo หลายเดือนก่อน +4

      @@dio56301 I doubt you're missing anything other than preferences. I can say, when I first started coding try {await x()} catch {} was easier to write and understand than .then(). That being said, this methodology in the video is fascinating and I will have to give it a try.

  • @risitas5874
    @risitas5874 2 หลายเดือนก่อน +27

    It's interesting for sure. Although I think all we've really done here is inverted the try-catch.
    You're still handling the error at the top with if(error), and the else block is like the old "try"

    • @ojikutu
      @ojikutu 2 หลายเดือนก่อน +3

      We've also managed to escape the variable scoping of try-catch

    • @Blafasel3
      @Blafasel3 2 หลายเดือนก่อน +3

      And you can do an early exit on the error. One issue with try catch is that it inverts the exit early pattern that is very common place.

    • @risitas5874
      @risitas5874 2 หลายเดือนก่อน +1

      @@ojikutu Not really because he's basically just moved the scoping to the else block.
      You wouldn't want to access the user variable outside the else block anyway

    • @lilyydotdev
      @lilyydotdev หลายเดือนก่อน +1

      @@risitas5874 That's literally not how scoping works or what that means. Besides, as he also pointed out in the video, the only reason he is using the else block at all is because he was not inside of a function and could not early-return.

  • @Gabriel-iq6ug
    @Gabriel-iq6ug 2 หลายเดือนก่อน +18

    It’s fascinating to explore this possibilities, but I strongly recommend sticking to the try-catch approach 99% of the time, reserving the techniques discussed in this video for a few highly specific cases.

    • @goosybs
      @goosybs หลายเดือนก่อน

      Why though? There a huge library building on this concept (EffectTS) and golang does this already by default. Also the inconvenience of try/catch is one of the two reasons why people still write:
      data = await fetch(...)
      result = await data.json()
      This should throw a compile error and be red in your editor, but it isn't. JS/TS should know that this throws atleast 2 errors and expect you to handle them. There's even a TC39 proposal to add this as a new keyword (behaviour). While it doesn't solve the issue that the language doesnt know what could throw (EffectTS solved this as best as it could), its still very encouraging to actually handle errors properly with this. So i'm really interested in why you would not recommend it...

  • @chris94kennedy
    @chris94kennedy 2 หลายเดือนก่อน +115

    in my opinion this is not the best advice, because this is idiomatic with Go where it forces you at a langauge (& LSP) level to consume the error returned in some way but forcing this idom into typescript - which dont get me wrong i have done similar in the past as well - risks inconsistent implementation which means error states not interupting the control flow in the right way at the right times.
    Forcing idioms doesn't always make sense even if they yield nicer syntax.
    EDIT: Also worth thinking about the several ways that error propagation can 'escape' the promise chain depending on what async you're doing (not just promise based), whether its unawaited+not returned, using timeouts that aren't themselves promise wrapped and awaited etc, which would make your errors evade your generic catch block, ending up probably at the process level or at least far away from its lexical context. Then you're either doing a bunch of nested calls to your wrapper, or just doing some nested .catch or even worse relying on process.on() captures, or something else depending on context, which ends up with a messy mix of paradigms.
    Imo it's just better to stay within the language idioms for predictability. Just my opinion though. If you want to write in languges that reliably return errors, and force you to consume those errors, use those languages. Typescript might have foibles in errors particularly in some async operations but this just seems like asking for trouble in production code.
    In other words, idioms exist in langauges in the context of how that language operates. Just because you can replicate and 'paste over' 90% of the behaviour through functions in another language, does not mean that you should. That missing 10% is going to cause hella problems and if you work in a consequential domain then that's not really a good idea, just for the sake of some opinionated preference on syntax.
    Just learn async really well, learn the event loop, handle it all idiomatically, and you're golden.

    • @amineabdz
      @amineabdz 2 หลายเดือนก่อน +2

      it's a different perspective, it makes it a little bit easier to write the flow of the logic without try catch, but you would need to handle the error flow yourself

    • @chris94kennedy
      @chris94kennedy 2 หลายเดือนก่อน

      @@amineabdz i think you're missing what im putting down

    • @jsvrs
      @jsvrs 2 หลายเดือนก่อน +3

      it's hard to read. try adding some line spaces.

    • @chris94kennedy
      @chris94kennedy 2 หลายเดือนก่อน +1

      @@jsvrs fair enough, have done so

    • @justsomeguy8385
      @justsomeguy8385 2 หลายเดือนก่อน +4

      I somewhat agree. It's still JS, and as long as you can enforce this pattern using a linter rule and CI job, any developer who isn't completely useless can learn the pattern intuitively in literally an hour.

  • @pu239
    @pu239 2 หลายเดือนก่อน +14

    This is how go handles errors by default, very pleasant

    • @chijiokejoseph7
      @chijiokejoseph7 หลายเดือนก่อน +2

      Yh it is, just that you return the error second and not first

    • @darian.rosebrook
      @darian.rosebrook 29 วันที่ผ่านมา

      @@chijiokejoseph7Lol if you’re using JavaScript, probably safe to assume you’re going to get a lot of errors at first

  • @pomprocks
    @pomprocks 2 หลายเดือนก่อน +15

    3:00 that isn't the only way to deal it it. You can use tools like "instanceof" in the catch to determine what type of error was thrown and deal with them in different ways just like you ended up doing it in a convoluted way.

    • @RmonikMusic
      @RmonikMusic 2 หลายเดือนก่อน +1

      Agreed. The only thing he's solving is not having to define the variable as a "let", using a "const" instead. But a const with extra type checking after is infinitely worse.
      Besides, if you write proper functional code, you usually want to return the value at the end of your try block, in which case you can just assign the value to a const wherever you wanna call the function. So in reality, the situation he's trying to solve it not even a valid one.

  • @ericmackrodt9441
    @ericmackrodt9441 2 หลายเดือนก่อน +62

    The catchError funcion is exactly what golang does.
    I tried to introduce that in a previous job and I got so much push back that I had I to remove all of it.
    The only annoying thing is that, unless you use let, if you have multiple of those calls I the scope, you'll have to name the error const something different in every call.

    • @krtirtho
      @krtirtho 2 หลายเดือนก่อน +18

      Once a wise man said, "Don't bring one language's convention in another language"

    • @Nguyen-c7z
      @Nguyen-c7z 2 หลายเดือนก่อน +3

      Cause it's honestly stupid. No proper developer write try catch like that anyway. This is junior example.

    • @chris94kennedy
      @chris94kennedy 2 หลายเดือนก่อน +1

      it is *not* exactly what golang does.

    • @ericmackrodt9441
      @ericmackrodt9441 2 หลายเดือนก่อน +2

      @@chris94kennedy Sure, it's not native to the language, and golang doesn't return an array, it can return multiple values in the same funcion.
      But in terms of flow, it's pretty much the same.

    • @chris94kennedy
      @chris94kennedy 2 หลายเดือนก่อน

      @@ericmackrodt9441 no - it is not. Please go and read my main comment on this video where i lay out why this is not the same - despite *looking and feeling the same in many/most cases* as golang.

  • @johnlagnese7076
    @johnlagnese7076 2 หลายเดือนก่อน +96

    "Ew, we have to move things into this try/catch. That's too much work!"
    /proceeds to write a complicated generic function that mixes results with operation state

    • @johnridout6540
      @johnridout6540 2 หลายเดือนก่อน +14

      It's actually a very simple function. Here's a pure JavaScript version.
      const catchError = promise => promise
      .then(data => [undefined, data])
      .catch(error => [error])

    • @josephvictory9536
      @josephvictory9536 2 หลายเดือนก่อน +1

      @@johnridout6540 Yes, im thinking of implementing this right now. I was quitely annoyed at the try catch situation i had before and this is a good solution.

  • @MatTeague
    @MatTeague หลายเดือนก่อน +1

    Thanks for the video and great idea. I have to use a lot of trycatch in my job so this was really interesting. I would really like a video on Effect as it looks cool

  • @EricSundquistKC
    @EricSundquistKC 2 หลายเดือนก่อน +38

    If you have small functions which only do one thing, then you won't have a bunch of code tryign to interact with your user right there immediately after you fetch it. Your try catch can just be in the fetchUser function.

    • @Jajoo13
      @Jajoo13 2 หลายเดือนก่อน +1

      But then you have to somehow tell in the return value that what you returned is coming from an error. I know you can let this slip sometimes but this approach can't be used universally.
      For example - say I have a method that gets something from the server and this something is an array of some items. Yes, you can always pass an empty array as a fallback but that does not say anything about the way of getting the data. What if you want to let user know that there's been an error getting the data and that's why they're seeing an empty list?
      Sure, you can display the message in the try/capture block of this method but unless you have some kind of centralized error handling, most times this will not be the case.

  • @Smoljames
    @Smoljames 2 หลายเดือนก่อน +14

    An interesting approach but perhaps this is fixing smth that isn't broken?

    • @Jajoo13
      @Jajoo13 2 หลายเดือนก่อน

      This is no fix - it's an improvement over standard try/catch.

  • @sfalpha
    @sfalpha 2 หลายเดือนก่อน +2

    You can simply add add bracket for the let scope variable.
    Try catch not meant to catch all error except the outermost one. You catch known errors and handle it accordingly.
    If you comes from Java you know that's well how Try/Catch is suppose to work. The exception is try/catch need to carefully handle the asynchronous situation.

  • @nathanl6598
    @nathanl6598 หลายเดือนก่อน +4

    The correct solution is to try catch the fetch statement and return undefined if it fails instead of the user data.
    What you're suggesting instead is a hacky version of a callback statement from the era before promises. We moved past that for a lot of good reasons. One of the problems you are going to face is if you do this twice in the same block of code and now have two variables named error. So now you're naming your errors things like error1 and error2? What is this recommendation.

  • @gearboxworks
    @gearboxworks หลายเดือนก่อน

    Congratulations; you just reinvented the Golang much despised error-handling pattern! (which I happen to appreciate and prefer.)
    And I mean congratulations sincerely, not sarcastically as try-catch used throughout a codebase is the root of all evil.

  • @samjohnston88
    @samjohnston88 2 หลายเดือนก่อน +61

    This doesn’t make sense. We have built ins for this. Use a try/catch and rethrow the error. You can provide a cause in the options if you want access to the original. In TS you can cast the error as unknown and you’ll be forced to check the type too. Sure abstract that into a function if you want something reusable but use the languages patterns and conventions

    • @chris94kennedy
      @chris94kennedy 2 หลายเดือนก่อน +4

      thank you for a sane comment. use the idioms that exist in the language because those idioms rely on the underlying mechanics to actually make sense. Just because you can get 80-90% of the way towards replicating a language behaviour from go in typescript does not mean you should, there are many ways this guy's code is going to go wrong. See my main comment for detail. It's sad to see a so-called senior engineer promote this imo because half the comments are like 'wow thats amazing im going to adopt this'. Enjoy the footguns.

    • @Kriszzzful
      @Kriszzzful 2 หลายเดือนก่อน

      So how would that abstracted function look like exactly? I am genuinely trying to understand. I understand you prefer a nested try catch over the Promise.then, but what is you return type exactly?

    • @Kriszzzful
      @Kriszzzful 2 หลายเดือนก่อน +4

      @@chris94kennedy a good idea will spread across languages until it gets native implementation. I am not familiar with Go syntax but I found this abstraction readable. Again you are complaining about footguns that already exist, whether you use this approach or not. What exactly would you suggest alternatively?

    • @chris94kennedy
      @chris94kennedy 2 หลายเดือนก่อน

      @@Kriszzzful i wrote a full separate comment on this video that makes my points so i dont feel the need to rewrite it and I'm not talking about syntax, in fact to the contrary ive said that that is the benefit of this approach but leads to issues. I have already made a suggestion so not sure why you're asking for it again, I said learn all the different forms and nuances of async in JS and how to write idiomatic error handling for all the edge cases.

    • @gearboxworks
      @gearboxworks หลายเดือนก่อน

      Many great ideas that emerge after entrenched dogma appear not to make sense to the majority at first so the majority are very critical when first proposed.
      But then more and more people start to consider that maybe the new approach is in-fact better than the previously assumed best practice. Over time, the majority go from being critics of the new approach to becoming advocates as the approach becomes mainstream.
      I watched this happen with classic object-oriented programming vs. containment and delegation, and I expect to see the same happen over the coming years regarding exception handling (try-catch/throw) vs. errors-as-values. #fwiw

  • @alii4334
    @alii4334 2 หลายเดือนก่อน +104

    I prefer "withCatch" as name to tell it is a wrapper

    • @dmltdev
      @dmltdev 2 หลายเดือนก่อน +2

      Do you mean something like that?
      function withCatch(fn, errorHandler) {
      try {
      const result = fn();
      if (result instanceof Promise) {
      return result.catch(errorHandler);
      } else {
      return result;
      }
      } catch (error) {
      errorHandler(error);
      }
      }

    • @dmltdev
      @dmltdev 2 หลายเดือนก่อน

      Misunderstood at first. Disregard pls.

    • @MerthanMerter
      @MerthanMerter หลายเดือนก่อน

      i prefer $catch

    • @4sxS307cAW
      @4sxS307cAW หลายเดือนก่อน +1

      i prefer create a catch factory. we are not the same.

  • @gasparsigma
    @gasparsigma 2 หลายเดือนก่อน +1

    This is useful in an abstraction like your react-query fetcher that can give you the data or handle the error headers/body for you. For everything else try/catch is easier to read

  • @jakeave
    @jakeave 2 หลายเดือนก่อน

    Thanks for including the types! There's pros and cons to wrapping error handling in every request. I usually end up having a single try catch but the catch calls the application error handler with 10 or so Error types.

  • @rufio.tf2
    @rufio.tf2 2 หลายเดือนก่อน +6

    Effect also has an "effect/Micro" component that seems like a reasonable choice if we're mostly trying to get the benefits of the core Effect type and not all of the other powerful offerings that come with the full library. I'm still getting a feel for Effect since fp-ts announced the merger with it.

    • @ChristopherBloom
      @ChristopherBloom หลายเดือนก่อน

      Hold up. fp-ts is merging with Effect?!

    • @PeterSahanaya
      @PeterSahanaya 16 วันที่ผ่านมา

      yep,i also use Effect/Micro and judt import the things i need like gen, try, tryPromise etc

  • @kevs3654
    @kevs3654 หลายเดือนก่อน

    I like this approach, already using it :)
    as golang lover, this approach is the best, less tab more readable

  • @Pluvo2for1
    @Pluvo2for1 2 หลายเดือนก่อน

    Thank you Kyle. You're a good guy

  • @Takatou__Yogiri
    @Takatou__Yogiri 2 หลายเดือนก่อน +23

    Why do i need try catch nesting? Because error.message tells what is the problem with the code. Its not like it shows completely unreadable error.

    • @Kriszzzful
      @Kriszzzful 2 หลายเดือนก่อน

      Because some errors inside a try you can handle but others should throw? How would you achieve that in a single try catch?

    • @hombacom
      @hombacom 2 หลายเดือนก่อน +2

      I see try/catch as few critical unexpected errors and limited scope, known errors should be handled with if, userExists(), result is null etc. It feels wrong to overengineer or add more libraries into the picture when you can simplify instead.

  • @BorisBarroso
    @BorisBarroso 2 หลายเดือนก่อน +15

    I saw that Theo showed something similar and this comes from go which manages errors this way. You explained this really well and I will copy and adapt your code to my needs. Thanks 🙏

  • @AlexBojenko
    @AlexBojenko 2 หลายเดือนก่อน +2

    It all started with treating "not exceptional case" as exception. "User does not exist" is a regular workflow. Exception is when you can not connect to the server. From there it all went south. Some weird spaghetti code with promises instead of good old try/catch. Exception is designed to handle the error and exit the function. If you catch it and continue, something is fundamentally wrong.

    • @ecavero1
      @ecavero1 หลายเดือนก่อน +1

      Agreed. No point in continuing if user does not exists, though.

  • @torickjdavis
    @torickjdavis 2 หลายเดือนก่อน +3

    Very similar to the proposal for the safe-assignment operator, which has been modified to try-expressions instead. So we might get some syntax for something like this in the future.

  • @AndresJimenezS
    @AndresJimenezS 2 หลายเดือนก่อน

    Effect is awesome, more content about this would be terrific

  • @dy4914
    @dy4914 2 หลายเดือนก่อน +1

    lad has reinvented golang error handling

  • @ransomecode
    @ransomecode 2 หลายเดือนก่อน +2

    Promise.try will officially will be out soon! It's pretty amazing!

  • @Tarabass
    @Tarabass 28 วันที่ผ่านมา

    Try catch is a low level pattern and can be used perfectly for catching errors. But, you have to type your errors.
    In that case you get multiple catches for every error type or an if statement in your catch to check the error type.

  • @ShahzadKhan1234
    @ShahzadKhan1234 2 หลายเดือนก่อน

    I just did command + s while watching your code 😁

  • @MrMetalGabriel
    @MrMetalGabriel 6 วันที่ผ่านมา

    It's a good advice, it's work make it simple... Declaring try/catch block many times in the same function sometimes is boring!

  • @dcernach
    @dcernach 2 หลายเดือนก่อน

    Basically it is a Result pattern in javascript. Great video by the way! I''ve been using something like this for a while!

    • @-taz-
      @-taz- 2 หลายเดือนก่อน +1

      Or std::optional in C++ that was from Alexandrescu's 2001 book that became the basis for Result. But I think this is less good than either, and more like Go's error handling, but also worse than that. Interesting, and possibly useful. More like a change of pace than any real benefits.

  • @PepereWorld
    @PepereWorld 12 วันที่ผ่านมา +1

    "try and catch" role is to catch Exception, "unexpected behavior" like network loss, i/o problems, etc.. Not for typo or error made by developer.
    In PHP, if you try to print_r() a undefined variable, it will raise an Warning error. In Java, because of compilation, your code would not even compile at all because to try to do something with undefined variable.
    In JS, in your example, it will failed because your variable is defined in the scope of the "try and catch" ... again, it's a language feature ...
    So, the problem here are about the pro and cons of how scopes are generally handled on the language level (just compare php/js/python per example).
    Until TypeScript become a real language on its own, you'll have live with the fact that is JavaScript is behind executing it ...
    But, I love it for what it offerts :)

  • @victoreleanya4375
    @victoreleanya4375 2 หลายเดือนก่อน

    fantastic, i love this. I learned something new

  • @Gabriel-iq6ug
    @Gabriel-iq6ug 2 หลายเดือนก่อน

    Your filming approach suggests a thorough understanding of the subject matter.

  • @micknamens8659
    @micknamens8659 หลายเดือนก่อน

    The motivation in this video is to have specific error handling depending on which of the called functions failed.
    We do this in the big common catch block by testing the error class (if necessary). Writing log output is already done in the called functions.
    The only distinction on caller side is whether to retry or whether to continue with a warning vs. rethrow the exception.

  • @maran.ath4
    @maran.ath4 2 หลายเดือนก่อน +3

    This is not an issue with try catch, it's a problem with how you think about architecture, usually no one would store a global user outside their function and set it's value later, you could've literally had the try catch inside the getUser function and return a user from the get user function and do any other error handling in the catch block, when you're writing actual business logic, that repository code will obviously be talking to an external service and "returning" something, not setting a global variable, then some other function in your application logic will call the repository and the getUser will return a user type, if there's any specific error handling try catch will still do the same thing, what you did was to write boilerplate for wrapping then catch

  • @JLuisEstrada
    @JLuisEstrada หลายเดือนก่อน

    This is like one of those infomercials in where clumsy people dont know how to use certain appliance or device, and then comes the savior with a tries to solve a problem that no-one has

  • @darshanjayadev
    @darshanjayadev 2 หลายเดือนก่อน

    Thanks!

  • @TannerBarcelos
    @TannerBarcelos 2 หลายเดือนก่อน +56

    I fell in love with this approach in Go. So glad this is being adopted into the Typescript ecosystem. It makes so much sense.

    • @Gakai08
      @Gakai08 2 หลายเดือนก่อน +12

      I like the Rust approach better, just return Result | Error, and then you have to narrow away the Error type to continue using the Result value.

    • @TannerBarcelos
      @TannerBarcelos 2 หลายเดือนก่อน

      @@Gakai08 interesting. I have never used Rust, but I’ve heard this same point from many others. I’ll take a look.

    • @legends_assemble4938
      @legends_assemble4938 2 หลายเดือนก่อน +4

      ​@@Gakai08yes, error handling *feels* better in rust when compared to go. But both the languages embrace errors as values approach, so it's not that much different, just different syntax or pattern.

    • @wilsonmela6343
      @wilsonmela6343 2 หลายเดือนก่อน

      ​@@legends_assemble4938 Exactly

    • @samuraijosh1595
      @samuraijosh1595 2 หลายเดือนก่อน

      it goes like this Haskell/Ocaml features -> Rust and Scala adopt them -> more mainstream and normie languages Js, go, pyton adopt them.

  • @refeals
    @refeals 2 หลายเดือนก่อน +7

    Supabase's API works kinda like that. I dont dislike it, but it can get messy with multiple promises with multiple arrays with errors. I guess the best solution depends on your project.

  • @seanZG
    @seanZG 2 หลายเดือนก่อน

    Yes please, would love a tutorial on Effect.

  • @Suragato
    @Suragato 16 ชั่วโมงที่ผ่านมา

    After working in go for a bit, I've come to like the multiple return values with the second value being an optional error. Just check if the error was returned and handle it there. if you return a value regardless of an error, it makes the code even safer.

  • @JoDraws
    @JoDraws 2 หลายเดือนก่อน

    Very clean

  • @killerpixel2k3
    @killerpixel2k3 2 หลายเดือนก่อน

    It's the same way the supabase JS SDK handles this issue. They return an object not array but otherwise it's the exact same idea! Great to see how it actually works for anything you want it to, rather than a specific SDK!!!

  • @frisedel
    @frisedel 2 หลายเดือนก่อน

    well that's it, I'm adding this to my python code. I need that since I have raise error in multiple places :) thanks!

  • @coltenkrauter
    @coltenkrauter 8 วันที่ผ่านมา

    This is solid.

  • @js9170
    @js9170 2 หลายเดือนก่อน

    Effect tutorial would be great! 🎉

  • @mbalaganskiy
    @mbalaganskiy 2 หลายเดือนก่อน

    Yet another one - try few nested calls... Welcome back to try/catch

  • @benyaminyakobi3652
    @benyaminyakobi3652 2 หลายเดือนก่อน +1

    Nice, similar idea of error as a value is a built-in feature in Golang :)

  • @BudgieMind
    @BudgieMind 2 หลายเดือนก่อน

    good idea!

  • @KlethonioFerreira
    @KlethonioFerreira 2 หลายเดือนก่อน +5

    Doug's Coding Corner posted a video exactly about this 2 days ago.

  • @KostasOreopoulos
    @KostasOreopoulos หลายเดือนก่อน

    This is a long standing debate. Errors as throws or errors as values. If you want to adopt errors as values in javascript, you definitely can. Golang, instead of re-throwing, just bubbles the error, returning it.
    You can use this at the top level of your throw-rethrow stack, where you will not rethrow the error but handle it. I personally love the errors as values, but everything in Javascript ecosystem is throw based, so it will be a little bit hard.
    Of course if we get the ?= operator, then things could change.

  • @akam9919
    @akam9919 2 หลายเดือนก่อน

    Forget all the whiners, this great. I hate dealing with the scoping issues that come with try-catch, and I hate that in typescript, the error is basically untyped which, in fairness, it may actually be something you can't actually predict, but I'd rather know if a function call can throw or not, and kinds of errors it can throw.

  • @juan0338
    @juan0338 2 หลายเดือนก่อน +1

    Cool, but maybe a nice usability addition would be to have a result type (object). Returning an array will inevitably pollute the variable space if you need to use the catch function several times, you will end up with err,err2,err3, etc. You could also declare the results as variables instead of using const, do I don’t know if that is a thing

    • @bpaintx2
      @bpaintx2 2 หลายเดือนก่อน

      Or you could use meaningful variable names. Instead of the incredibly useless err, name it userFetchError.

  • @chrismalcheski9232
    @chrismalcheski9232 2 หลายเดือนก่อน

    You have GOT to be the closest thing humanity has to the perfect video creator. Clear, concise speech, beyond perfect presentation, fully planned and prepared lessons, and material so well written that it's mind-boggling. As if that weren't enough, there is also unparalleled depth of knowledge and understanding. A video of yours is the de facto standard for all others. NOBODY matches the quality of your videos on every conceivable front.

  • @fromant65
    @fromant65 2 หลายเดือนก่อน

    I've been working in web dev for a few months and I never had a problem with this kind of thing. Though we defined a similar function (tryCatch, which literally tries and catches the callback you pass to it) just to have a more modular code

  • @TheRicoco2011
    @TheRicoco2011 2 หลายเดือนก่อน +1

    Interrested by Effect.

  • @pineappl3pizzzasoju
    @pineappl3pizzzasoju 2 หลายเดือนก่อน +2

    Practically there should be error handling with try/catch in the getUser function to begin with and that would have solved all the problem you mentioned.

  • @woahitsraj
    @woahitsraj 2 หลายเดือนก่อน +7

    We would love a full Effect tutorial. We are thinking of adopting it!

  • @definitive_solutions
    @definitive_solutions 20 วันที่ผ่านมา

    To the ppl saying this would lead to having multiple constants named error, we could return either an Error or a result instead of an array, and verify if result instanceof Error or not

  • @tejus_sahi
    @tejus_sahi 2 หลายเดือนก่อน +1

    If you use try catch and throw new Error.
    You can also use console.log(error.message ?? error) in catch block.

  • @akshatmishra8664
    @akshatmishra8664 2 หลายเดือนก่อน +18

    This approach looks similar to how Go handles errors

    • @蕭宇廷-n4t
      @蕭宇廷-n4t 2 หลายเดือนก่อน +1

      Yeah, I was thinking the same...

    • @adtc
      @adtc 2 หลายเดือนก่อน +1

      Where do you think he got the idea from? Just another excuse to make a 10 minute video and collect more views.

    • @Jajoo13
      @Jajoo13 2 หลายเดือนก่อน

      @@adtc It doesn't really take a genius to discover this - I've been using this for years already. I do agree - lately I've been seeing more videos about this subject. It seems to be easily milkable so I guess it's because of that. But hey, it's a nice pattern so spreading the word is beneficial.

    • @zakarylittle6767
      @zakarylittle6767 2 หลายเดือนก่อน

      ​@@Jajoo13 A nice pattern if you like wasting time and money 😂

    • @Jajoo13
      @Jajoo13 2 หลายเดือนก่อน

      @@zakarylittle6767 ?

  • @seanki
    @seanki 2 หลายเดือนก่อน

    If you’re in a react project then react-query is top notch and has this functionality and much more built into hooks

  • @stephenb_dev
    @stephenb_dev 2 หลายเดือนก่อน +2

    Yeah No. This trend of ditching try-catch is funny.
    I remember the good old days when errors by value was big no no.
    What happened to keeping business logic separate from error management ?
    The main reason I don't like Go. No try-catch.

  • @irvintorres4939
    @irvintorres4939 หลายเดือนก่อน

    the golang way, very nice

  • @chriscordos5622
    @chriscordos5622 2 หลายเดือนก่อน

    😆 LoL, 🤦‍♂️ what a funny guy.
    The main reason try-catch sucks is when it's frivolously used to mitigate foul logical constructs, otherwise it's very necessary for errors that occur beyond your control (mostly external input).

    • @User948Z7Z-w7n
      @User948Z7Z-w7n 29 วันที่ผ่านมา

      It's a common pattern among data fetching libraries. const {loading,data,error}=useQuery(...

  • @gweltazlemartret6760
    @gweltazlemartret6760 2 หลายเดือนก่อน

    At 1:52 it’s slightly false.
    You can define a `user` outside try, then affect inside, then exit main path on the catch block, while keeping main path intact. 😊
    (Edit: Talked about at 3:02, nice.)

  • @Surefire99
    @Surefire99 2 หลายเดือนก่อน

    I've always wished languages had something like a data type of error or a class of error that had the special property that if you test against it, it always returns false. So a function call would return the normal value, or the error. And you just need to test against the return value to see if it worked. In cases of errors, the object would have properties for the error message, a stack trace, etc.

  • @dylan-j-gerrits
    @dylan-j-gerrits 2 หลายเดือนก่อน

    It's just a basic abstraction. I always use something similar to wrap my error sensitive block. It makes a lot of sense in middleware to just pass the error to a handler afterward. If not, it is just adding more control flow.

  • @garyduell3768
    @garyduell3768 2 หลายเดือนก่อน +1

    If I remember correctly, throws or exceptions inside of an async function or context might not actually fire the catch in the calling function.

    • @adonisengineering5508
      @adonisengineering5508 2 หลายเดือนก่อน +1

      There are other even subtler tricks that a real developer would know about, but typeshit will never let you even get near those edge cases, it is by idiots for idiots, playground guardrails.

    • @Daniel-zy1ir
      @Daniel-zy1ir 2 หลายเดือนก่อน

      @@adonisengineering5508 Let me guess, you code in Vim?

  • @alexeydugnist3520
    @alexeydugnist3520 หลายเดือนก่อน

    I vote for Effect tutor video👍

  • @Ruchunteur
    @Ruchunteur หลายเดือนก่อน +1

    isn't it essentially the same as putting a try catch in a separate function instead of writing the logic directly in the current function though ? I mean, I like that it make the original function more readable but you can write the very same logic with your withCatch function being async and using a try/catch/await inside just like in your original function and simply return the same thing you returned in your promise. The point is what your approach didn't really change anything about the usefulness of a try and catch, it's just moved the logic on a separate function but try and catch can be use in that separate function just the same.
    async function catchError(promise: Promise): Promise {
    try {
    return [undefined, await promise]
    } catch (error) {
    return [error]
    }
    }
    const [error, user] = await catchError(getUser(1))

  • @ThomasWSmith-wm5xn
    @ThomasWSmith-wm5xn หลายเดือนก่อน

    It feels very golangy and I love it.

  • @seccentral
    @seccentral 2 หลายเดือนก่อน

    reminds me of effect error handling (from the functional effect ecosystem of tools, not the react hook)
    Edit: now that i watched till the end, I can see you mentioned it too 😂

  • @vsaihruthikreddy7127
    @vsaihruthikreddy7127 หลายเดือนก่อน

    The video is well-explained, but the approach really depends on the nature of the task. For simpler tasks with fewer sections, using a basic try-catch in each section can be an effective and straightforward solution. However, in extremely complex situations, using catchErrorTyped with await works very well, as it provides more control and clarity in error handling. It’s about using the right tool for the job: just as you’d use a knife to cut vegetables, not a sword, a simple try-catch can be more suitable for simpler needs.

  • @AbsoluteWoo
    @AbsoluteWoo 2 หลายเดือนก่อน +46

    This video should actually being titled "I don't understand how to use Try/Catch properly"

  • @briginas
    @briginas 26 วันที่ผ่านมา

    'satisfies [undefined, T]' looks sexier than 'as [undefined, T]' :)

  • @kkingbd
    @kkingbd 2 หลายเดือนก่อน +1

    catch(error) there for a reason.
    You can get an idea about the error once you read the error.

  • @lilyydotdev
    @lilyydotdev หลายเดือนก่อน

    Personally if the function is one of my own - like a get_user() abstraction of a database call - I like to return errors in the form of an object like `{ error: Error_Enum; data: T }`. To handle exceptions the ORM or any other applicable external function calls might throw I definitely reach for a catch_error() style.

  • @mohit84604
    @mohit84604 2 หลายเดือนก่อน +3

    It's go syntax.

  • @rodrigopim7693
    @rodrigopim7693 หลายเดือนก่อน

    You can have several catches in a try code block and throw your exceptions by treating each case separately.

  • @captainkite
    @captainkite 2 หลายเดือนก่อน +9

    The reason you gave for not using a try catch doesn't make sense at all. While using try catch and logging the error you can print the actual error message by console logging the error plus your custom message. eg catch { console.log('Custom error message' : error)}. Your method is very obscure, I thought you were supposed to simplify the web for us, Kyle.

  • @thejimmylin
    @thejimmylin หลายเดือนก่อน

    Every time I see this it reminds me of this funny Go joke:
    Go always keeps two cups by the bedside. One cup has water, while the other is always empty.
    Rust asks puzzled: "Why do you always keep two cups here?"
    Go replies: "Because I need to drink water when I wake up at night."
    Rust follows up: "But why keep an empty cup?"
    Go: "What if one day I wake up and don't want to drink water?"

  • @ring11037
    @ring11037 2 หลายเดือนก่อน

    Thanks for helping me understand try{...}catch{...} better. :P
    I'm not sure if I will use this solution. I don't quite understand advanced fix yet but the basic fix is interesting, and it's reusable.

    • @arvi8843
      @arvi8843 2 หลายเดือนก่อน

      It's not a good solution in my opinion. Read other comments feedback on this same video. 😅

    • @ring11037
      @ring11037 2 หลายเดือนก่อน

      @@arvi8843 I'm not an expert at programming, i'm probably wrong. But you think it's a bad solution simply because most people say so?

    • @arvi8843
      @arvi8843 2 หลายเดือนก่อน

      @@ring11037 No. I'm just lazy at writing my explanation so I told you to read what other people say so. 🤣 Personally, I don't want my function calls wrapped around something like catchX(myFunction). The try/catch is enough if you know where to put it. Routers also have a "next" step func and catch all error handling. What he's showing here is just snippet of code not the actual arch design of the app.

  • @davidentzat5671
    @davidentzat5671 2 หลายเดือนก่อน

    Thanks for the great video. I'll be interested in the video about the Effect library, please

  • @abaking9611
    @abaking9611 2 หลายเดือนก่อน

    Interesting. Can you explain each part of the function declaration in the catchErrorTyped function. Some of it, I'm not sure I understand.

  • @T1Oracle
    @T1Oracle หลายเดือนก่อน

    This is why I prefer Rust! I can't wait until the day that I can use Rust for the front end of enterprise web applications without compromise.

  • @JoeBuza
    @JoeBuza 2 หลายเดือนก่อน +3

    Might as well use Effect

    • @muchirajunior
      @muchirajunior 2 หลายเดือนก่อน

      Now you are being stupid 😂😂😂

  • @Its-InderjeetSinghGill
    @Its-InderjeetSinghGill 2 หลายเดือนก่อน

    +1 I would love to learn more about Effect lib.

  • @RyotsukeHibiki
    @RyotsukeHibiki 2 หลายเดือนก่อน

    So re-inventented tanstack query wrapper for async calls?

  • @tjkatz
    @tjkatz 2 หลายเดือนก่อน

    Despite the comments. Things are moving in this direction. Plaid TS SDK returns { data, error } everywhere. They never throw errors and so you never need to catch. This is the way. Is this like Go? Yes, because Go is superior. However, when you can't use Go there is this approach.

  • @justsomeguy8385
    @justsomeguy8385 2 หลายเดือนก่อน +1

    Sure it's a better experience, but the language doesn't force it so almost no one will do this. New projects using this will eventually get developers who ruin the pattern, and now you're left with some places where errors are handled like this, some places with try/catch, and places where error catching doesn't happen at all
    This needs to be part of a library to be useful(I agree, effect is too bloated if you just want this), and there needs to be strict linter rules to prevent people from doing it any other way.

  • @RayBellis
    @RayBellis 2 หลายเดือนก่อน +4

    1. If your code blocks inside `try` are too long, then your code is doing too much. Refactor.
    2. The `Error` class can be easily subclassed, so you can throw application specific error types
    3. With subclassed errors you can easily do `if (error instanceof MyErrorClass)` and then rethrow the error if it doesn't match
    4. In the case of TypeScript, your `usr` syntax error will get caught by the compiler. This is more applicable to pure JS.
    5. Even in JS, your model won't catch a syntax error *in the called function* - it'll still throw that generic error.

    • @lilyydotdev
      @lilyydotdev หลายเดือนก่อน

      1. He did refactor
      2. He also did that
      3. He also did that, and abstracted it nicely
      4. Syntax errors were used as an example. Additionally, TSC is not actually a compiler and is far from perfect. Unexpected runtime errors are very real and abstracting frequently used `if (e instanceof MyErrorClass` is - as I've already said - a form of refactoring that makes handling custom error classes properly less of a chore.
      5. Correct, nothing more to say about this.

  • @geek-peek
    @geek-peek หลายเดือนก่อน

    There is a problem with catchError function: you don't actually flatten your code, because for each function call you have to add if-else statement. I use another solution: several custom errors extending Error. When I expect an error, I use try/catch and wrap an error inside my own error. Then it is being thrown up to presentation layer and transformed by custom filter, which applies logging and makes a client response based on error type and properties

  • @dweblinveltz5035
    @dweblinveltz5035 2 หลายเดือนก่อน +2

    JS should just incorporate a new "try/catch" that doesn't create a new scope. They don't even have to break the old version.

    • @dmltdev
      @dmltdev 2 หลายเดือนก่อน

      They seem to be already doing that with a new safe assignment operator `?=`, which is a TC 39 proposal (or other syntax they're currently selecting, like `await try`). It basically does the same, if I got it right.

  • @bharadwajvaranasi6447
    @bharadwajvaranasi6447 หลายเดือนก่อน

    He is the best programmer