如何正确处理IcedCoffeeScript错误? [英] How to correctly handle errors with IcedCoffeeScript?

查看:245
本文介绍了如何正确处理IcedCoffeeScript错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

node.js中的常见做法是将错误消息作为回调函数的第一个参数返回。在纯JS(Promise,Step,seq等)中有一些解决这个问题的解决方案,但是它们都不能与ICS集成。

It is common practice in node.js to return error message as the first argument to a callback function. There are a number of solutions to this problem in pure JS (Promise, Step, seq, etc), but none of them seem to be integrable with ICS. What would be correct solution to handle errors without losing much of readability?

例如:

# makes code hard to read and encourage duplication
await socket.get 'image id', defer err, id
if err # ...
await Image.findById id, defer err, image
if err # ...
await check_permissions user, image, defer err, permitted
if err # ...


# will only handle the last error
await  
  socket.get 'image id', defer err, id
  Image.findById id, defer err, image
  check_permissions user, image, defer err, permitted

if err  # ...


# ugly, makes code more rigid
# no way to prevent execution of commands if the first one failed
await  
  socket.get 'image id', defer err1, id
  Image.findById id, defer err2, image
  check_permissions user, image, defer err3, permitted

if err1 || err2 || err3  # ...


推荐答案

和编码惯例。它总是出现在所有的时间。让我们将您的代码段放在下面,更多一点,以便我们有一个可行的函数。

I solve this problem through style and coding convention. And it does come up all the time. Let's take your snippet below, fleshed out a little bit more so that we have a workable function.

my_fn = (cb) ->
  await socket.get 'image id', defer err, id
  if err then return cb err, null
  await Image.findById id, defer err, image
  if err then return cb err, null
  await check_permissions user, image, defer err, permitted
  if err then return cb err, null
  cb err, image

你是完全正确的,这是丑陋的,因为你在许多地方的代码短路,你需要记住调用cb每次返回。

You're exactly right, this is ugly, because you are short-circuiting out of the code at many places, and you need to remember to call cb every time you return.

您提供的其他代码段产生不正确的结果,因为它们会引入需要序列化的并行性。

The other snippets you gave yield incorrect results, since they will introduce parallelism where serialization is required.

我的个人ICS编码约定是:(1)从一个函数返回一次(该控制从结束);和(2)尝试在同一级别的缩进处理错误。以我喜欢的风格重写您的内容:

My personal ICS coding conventions are: (1) return only once from a function (which control falls off the end); and (2) try to handle errors all at the same level of indentation. Rewriting what you have, in my preferred style:

my_fn = (cb) ->
  await socket.get 'image id', defer err, id 
  await Image.findById id, defer err, image                   unless err?
  await check_permissions user, image, defer err, permitted   unless err?
  cb err, image

如果socket.get调用出现错误,你需要检查错误两次,它显然会失败两次。我不认为这是世界末日,因为它使代码更清洁。

In the case of an error in the socket.get call, you need to check the error twice, and it will obviously fail both times. I don't think this is the end of the world since it makes the code cleaner.

或者,你可以这样做:

my_fn = (autocb) ->
  await socket.get 'image id', defer err, id
  if err then return [ err, null ]
  await Image.findById id, defer err, image
  if err then return [ err, null ]
  await check_permissions user, image, defer err, permitted
  return [ err, image ]

如果你使用autocb,这不是我最喜欢的ICS功能,那么编译器会在你返回/短路出函数时调用autocb。我认为这种结构从经验更容易出错。例如,想象你需要在函数开始时获取一个锁,现在你需要释放它n次。其他人可能不同意。

If you use autocb, which isn't my favorite ICS feature, then the compiler will call the autocb for you whenever you return/short-circuit out of the function. I find this construction to be more error-prone from experience. For instance, imagine you needed to acquire a lock at the start of the function, now you need to release it n times. Others might disagree.

另一个注释,在评论中指出。 autocb 的工作原理像 return ,因为它只接受一个值。如果要返回多个值,如在此示例中,您需要返回一个数组或字典。 defer 会解构作业,协助您完成这项作业:

One other note, pointed out below in the comments. autocb works like return in that it only accepts one value. If you want to return multiple values as in this example, you need to return an array or dictionary. defer does destructuring assignments to help you out here:

await my_fn defer [err, image]

这篇关于如何正确处理IcedCoffeeScript错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆