Thanks to visit codestin.com
Credit goes to github.com

Skip to content

(Jason.DecodeError) unexpected byte at position 0: 0x3C ("<") crashed my program #40

@yeshan333

Description

@yeshan333

I built a telegram bot api proxy with cloudflare worker based on the open source project Telegram-API-Proxy, and when the proxy layer returned a message in non-json format, Jason.decode! crashed the whole program.

error throw in:

data = Jason.decode!(json, keys: :atoms)

** (Jason.DecodeError) unexpected byte at position 0: 0x3C ("<")
(jason 1.4.1) lib/jason.ex:92: Jason.decode!/2
(telegex 1.8.0) lib/telegex/caller/adapter.ex:46: Telegex.Caller.Adapter.struct_response/1
(telegex 1.8.0) lib/telegex/caller/adapter/httpoison.ex:25: Telegex.Caller.Adapter.HTTPoison.parse_response/1
(telegex 1.8.0) lib/telegex.ex:119: Telegex.get_me/0
(telegex 1.8.0) lib/telegex/instance.ex:48: Telegex.Instance.handle_call/3
(stdlib 5.2.3) gen_server.erl:1131: :gen_server.try_handle_call/4
(stdlib 5.2.3) gen_server.erl:1160: :gen_server.handle_msg/6
(stdlib 5.2.3) proc_lib.erl:241: :proc_lib.init_p_do_apply/3
Last message (from RssGenericI18nBot.Telegram.TelegramPoller.Supervisor): :fetch_me

I tried adding some debugging information to adapter.ex, and found that sometimes the proxy return [info] Would decode Telegram Response: error code: 520 file=lib/telegex/caller/adapter.ex line=46 application=telegex

  @spec struct_response(String.t() | map) :: Response.t()
  def struct_response(json) when is_binary(json) do
    Logger.info("Would decode Telegram Response: #{json}")
    data = Jason.decode!(json, keys: :atoms)

    struct(Response, data)
  end

Are there any good ways to deal with it?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions