twilio / twilio-java

A Java library for communicating with the Twilio REST API and generating TwiML.
MIT License
484 stars 425 forks source link

ApiException: Unexpected character ('<' (code 60)) #604

Closed jmart1 closed 3 years ago

jmart1 commented 3 years ago

Issue Summary

A summary of the issue and the environment in which it occurs. If suitable, include the steps required to reproduce the bug. Please feel free to include screenshots, screencasts, or code examples.

I have a Spring Boot backend application that sends SMS msgs via the Twillio Java SDK. The application was up for 17 days without a problem. Then in the middle of the day we start getting an exception thrown when calling MessageCreator.create(). There is only one instance of this application and it was occurring 100% of the time. The application did not run out of RAM or disk space. A restart of the application fixed it.

Steps to Reproduce

  1. This is the first step
  2. This is the second step
  3. Further steps, etc.

Code Snippet

# paste code here

    @Override
    public void sendSms(String telephoneNumber, String message) {
        if (!telephoneNumber.contains("+")) telephoneNumber = "+" + telephoneNumber;
        var smsMsg = Message.creator(
                new com.twilio.type.PhoneNumber(telephoneNumber),
                new com.twilio.type.PhoneNumber(fromNumber),
                message)
                .create();   //TwilioAdapter.java:41... see stack trace below
        log.debug("smsMsg.getSid() = {}", smsMsg.getSid());
    }

Exception/Log

# paste exception/log here

com.twilio.exception.ApiException: Unexpected character ('<' (code 60)): expected a valid value (JSON String, Number, Array, Object or token 'null', 'true' or 'false')
 at [Source: (ByteArrayInputStream); line: 1, column: 2]
        at com.twilio.exception.RestException.fromJson(RestException.java:57)
        at com.twilio.rest.api.v2010.account.MessageCreator.create(MessageCreator.java:505)
        at com.twilio.rest.api.v2010.account.MessageCreator.create(MessageCreator.java:25)
        at com.twilio.base.Creator.create(Creator.java:45)
        at net.pacnode.twilio.adapters.TwilioAdapter.sendSms(TwilioAdapter.java:41) 
.
.
.
Caused by: com.fasterxml.jackson.core.JsonParseException: Unexpected character ('<' (code 60)): expected a valid value (JSON String, Number, Array, Object or token 'null', 'true' or 'false')
 at [Source: (ByteArrayInputStream); line: 1, column: 2]
        at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1851)
        at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:707)
        at com.fasterxml.jackson.core.base.ParserMinimalBase._reportUnexpectedChar(ParserMinimalBase.java:632)
        at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._handleUnexpectedValue(UTF8StreamJsonParser.java:2686)
        at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._nextTokenNotInObject(UTF8StreamJsonParser.java:865)
        at com.fasterxml.jackson.core.json.UTF8StreamJsonParser.nextToken(UTF8StreamJsonParser.java:757)
        at com.fasterxml.jackson.databind.ObjectMapper._initForReading(ObjectMapper.java:4662)
        at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4511)
        at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3503)
        at com.twilio.exception.RestException.fromJson(RestException.java:55)
        ... 86 common frames omitted

Technical details:

jmart1 commented 3 years ago

Just to add to this. We have two messages we send. This was occurring on both of the messages.

eshanholtz commented 3 years ago

@jmart1 You stated a restart of the application fixed it, have you been experiencing this error since? I'm not able to recreate this issue.

Based on the stack trace, it looks like the application successfully sent the API request to Twilio, but wasn't receiving a successful status code in the response, which is why the exception occurs on line 505 in MessageCreator.java when it tries to convert the HTTP Response body to JSON. If you're still seeing this issue, I recommend catching the ApiException and trying to read the raw response body to figure out why it's not parsing the JSON response properly. If you need additional help, please post any further response details you uncover.

jmart1 commented 3 years ago

Thank you so much for looking into this and writing back.

have you been experiencing this error since?

It hasn't occurred again.... though the application was up 17 days before it occurred.

Based on the stack trace, it looks like the application successfully sent the API request to Twilio

I notice that these failed requests do not show up in the logs on the Twilio dashboard. I was able to find in the logs the last successful request before this issue occurred, and the first successful after I restarted the application. The failed requests during the outage do not show up in the logs. Maybe that's a useful clue.

I recommend catching the ApiException and trying to read the raw response body to figure out why it's not parsing the JSON response properly.

Thanks for the advice. I will do that.

thinkingserious commented 3 years ago

Hi @jmart1,

It looks like you have a path forward, so I am closing this issue; however, if you require further assistance on this issue, please reopen. Thanks!