Golang mgo错误(Golang mgo errors)

编程入门 行业动态 更新时间:2024-10-27 02:21:58
Golang mgo错误(Golang mgo errors)

mgo可以返回与QueryError或ErrNotFound不同的错误吗? 数据库连接错误是什么?

在ErrNotFound之外发生的错误恐慌是否是一种好的做法,并且在状态为500的情况下在http处理程序堆栈顶部恢复类似的响应?

Can mgo return error different than QueryError or ErrNotFound? What with database connection errors?

Is it a good practise to panic on error different than ErrNotFound and recover on the top of http handlers stack with something like pretty response with status 500?

最满意答案

由mgo返回的一组错误不受限制,因为它在底层执行了一些操作,可能还会返回错误(DNS解析,连接建立,超时等)。 因此,用mgo处理错误的正确方法与大多数地方相同:处理那些你知道的并且具有自定义逻辑的人,并且对那些你不知道的人进行纾解。 良好的救援包括撤消任何本地副作用(关闭/删除本地创建的文件等),然后将错误返回给调用者,可能用自定义上下文信息进行修饰或包装。

我不会惊恐于这样的错误。 例如,当开发者在API上做了错误的事情,或者环境严重受损,并且最好的行动方式是完全停止时,恐慌通常用于异常情况。 与数据库(或任何与网络相关的)的连接应该每隔一段时间就会崩溃,并且适当地处理,而不是只记录一次不可区分的崩溃。

如果你有更多的细节,并希望进一步交谈,请进入邮件列表。

The set of errors returned by mgo is not constrained, because it does a number of operations underneath that may also return errors (DNS resolution, connection establishment, timeouts, etc). So the proper way to handle errors with mgo is the same as most places: handle the ones you do know about and have custom logic for, and bail out on the ones you don't. Good bailing out encompasses undoing any local side-effects (close/remove locally created files, etc), and then returning the error to the caller, perhaps decorated or wrapped with custom context information.

I wouldn't panic on such errors. Panics are usually for abnormal situations, when the developer did something wrong with the API, or the environment is seriously damaged and the best course of action is to stop altogether, for example. A connection with the database (or anything network related) should be expected to fall down every once in a while, and handled appropriately rather than just logging an undistinguishable crash.

If you have more details and would like to talk further, please come over to the mailing list.

更多推荐

本文发布于:2023-08-05 23:01:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1441428.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   Golang   mgo   errors

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!