简体   繁体   English

Elixir / Phoenix LiveView:如何向Rollbar报告异常?

[英]Elixir / Phoenix LiveView: How can I report exceptions to Rollbar?

I've had a lot of fun playing around with the still-beta Phoenix LiveView library. 使用仍然测试版的Phoenix LiveView库,我玩得很开心。 One thing that I'm confused about is how to ensure that my LiveViews will behave in a predictable and transparent way when an exception occurs. 我感到困惑的一件事是,如何确保发生异常时,LiveView能够以可预测和透明的方式运行。

In a conventional HTTP-request-based world, I'd expect that particular request to crash, returning a 500 response and possibly some error page that indicates to the user "Hey, there's a problem with the server". 在传统的基于HTTP请求的世界中,我希望该特定请求崩溃,返回500响应以及可能向用户指示“嘿,服务器有问题”的错误页面。 And the developer can count on integrations such as Rollbax detecting and reporting on the exception, so I get an email about the error and can inspect it in the Rollbar dashboard. 开发人员可以依靠诸如Rollbax这样的集成检测和报告异常,因此,我会收到有关该错误的电子邮件,并可以在Rollbar仪表板中对其进行检查。

But when my LiveView code crashes, I don't get this assurance. 但是当我的LiveView代码崩溃时,我没有得到保证。 The LiveView process will recover from the crash and (as I understand it) revert to its last known healthy state, which is really cool but also could invite user frustration (eg "Every time I submit your form, it shows me a loading icon then reverts to the blank form"). LiveView进程将从崩溃中恢复,并且(据我所知)恢复到其上一个已知的正常状态,这确实很酷,但也会引起用户的沮丧(例如,“每次我提交表单时,都会向我显示一个加载图标,然后还原为空白表格”)。 And I'm unclear how I could detect or catch exceptions in my LiveView so that they can be reported to an error monitoring service like Rollbar. 而且我不清楚如何在LiveView中检测或捕获异常,以便可以将异常报告给Rollbar之类的错误监视服务。 (Short of impractical solutions like adding a rescue to every handler) (简短的不切实际的解决方案,例如向每个处理程序添加rescue

How can I detect exceptions that happen in a LiveView so that I can a) somehow alert the end user of the problem, and b) ensure the exception is reported to the developer? 如何检测LiveView中发生的异常,以便a)以某种方式提醒最终用户该问题,以及b)确保将异常报告给开发人员?

But when my LiveView code crashes, I don't get this assurance. 但是当我的LiveView代码崩溃时,我没有得到保证。 The LiveView process will recover from the crash and (as I understand it) revert to its last known healthy state LiveView进程将从崩溃中恢复,并且(据我所知)恢复到其最后一个已知的健康状态

The way LiveView handles recovery is by having the clent start a new LiveView process. LiveView处理恢复的方法是让客户积极地启动新的LiveView过程。 So in case of errors, the current LiveView process will actually crash, which will send a message to Elixir's Logger, which is most likely intercepted by Rollbax (I assume it has Logger integration). 因此,在出现错误的情况下,当前的LiveView进程实际上将崩溃,这将向Elixir的Logger发送一条消息,该消息很可能被Rollbax拦截(我认为它已集成Logger)。 So everything should work when it comes to error handling. 因此,当涉及到错误处理时,一切都应该正常工作。

How can I detect exceptions that happen in a LiveView so that I can a) somehow alert the end user of the problem, and b) ensure the exception is reported to the developer? 如何检测LiveView中发生的异常,以便a)以某种方式提醒最终用户该问题,以及b)确保将异常报告给开发人员?

The plan is that an error on connected mount will refresh the page, forcing the request to go through the regular HTTP-connected request which will then raise like it does outside of LiveView. 计划是在连接的挂载上出现错误将刷新页面,从而迫使请求通过常规的HTTP连接的请求,然后该请求将像在LiveView外部一样发出。 An error on any other handler will cause a reconnection, which will then trigger a connected mount (which then may also fail and fallback to HTTP). 任何其他处理程序上的错误都将导致重新连接,然后重新连接,这将触发连接的挂载(然后可能还会失败并回退到HTTP)。 But in all of those cases, there is a process, so the errors should be arriving to Rollbax. 但是在所有这些情况下,都有一个过程,因此错误应该到达Rollbax。

One might override mount/2 callback. 一个可能会覆盖mount/2回调。 From Phoenix LiveView docs: 从Phoenix LiveView文档:

If at any point during the stateful life-cycle a crash is encountered, or the client connection drops, the client gracefully reconnects to the server, passing its signed session back to mount/2 . 如果在有状态生命周期的任何时候遇到崩溃,或者客户端连接断开,则客户端会正常重新连接到服务器,并将其签名的会话传递回mount/2
https://github.com/phoenixframework/phoenix_live_view/blob/master/lib/phoenix_live_view.ex#L150-L153 https://github.com/phoenixframework/phoenix_live_view/blob/master/lib/phoenix_live_view.ex#L150-L153

That said, if you expect your LiveView process to become zombie for some reason, you might eg maintain the list of connected sockets alongside with timestamps and alarm if reconnects happen in not expected way. 就是说,如果您希望LiveView进程由于某种原因而变得僵尸,则可以例如维护连接的套接字列表以及时间戳,并在发生意外连接时发出警报。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM