CityBaseInc / airbrake_client

Airbrake client to report errors and exceptions to Airbrake.io.
Other
0 stars 1 forks source link

Warnings and errors if missing JSON encoder #31

Closed jdfrens closed 4 months ago

jdfrens commented 4 months ago

If a JSON encoder is not defined, sending an Airbrake notice can crash your app.

We removed poison as a dependency in one of our apps and did not set :json_encoder in the config for :airbrake_client. The code compiled fine and everything worked fine until Airbrake.Worker tried to JSON encode the payload. Since :json_encoder was not set, Airbrake.Worker tried using Poison, but since that had been removed as a dependency, there was no module for JSON encoding. Airbrake.Worker threw an exception which caused its process to crash, and subsequently caused the whole app to crash.

It's a silent failure unless you happen to be watching the logs or if you monitor the state of your app (which is probably a good idea anyway).

This PR tries to solve two problems:

If :json_encoder is not set and poison is not a dependency, airbrake_client will compile with a warning. This is pretty weak, but it's was the best I could do with my meta-programming skills.

Screenshot 2024-05-10 at 11 10 57 AM

If :json_encoderis not set andpoisonis not a dependency,airbrake_clientwill not start and so will prevent your app from starting. The check is inAirbrake.Worker.init/1`.

Screenshot 2024-05-10 at 10 45 05 AM

If a module for a JSON encoder is available but it does not define encode!/1, there will be a runtime error to stderr (not using the logger) and a very simple Airbrake notice about the missing encode!/1 will be sent. The Airbrake.Worker process will not crash.

Screenshot 2024-05-10 at 10 46 45 AM