0

I am new to Apex and SF and I have to manage some errors from an 3rd party API we are calling. I have been using DTO with very simple response objects and the error response object we get back has one more level.

JSON response from API:

{
    "error": {
        "message": "File couldn't be downloaded: main_panel"
    }
}

DTO file:

public class CreateCadRequestError {
        public string error;
        public CreateCadRequestErrorMessage message;
    }
    
    public class CreateCadRequestErrorMessage {
        public string message;
    }

Apex class deserializing data

class SoloConnector {
    resp = makeNetworkCall()...
    SoloDTO.CreateCadRequestError resp = ( SoloDTO.CreateCadRequestError )JSON.deserialize( response.getBody(), SoloDTO.CreateCadRequestError.class );
    System.debug('resp: ' + resp.error + resp.message);

}

I am getting an error

illegal value for primitive

I assume its my DTO class and not being able to serialize the data. Any suggetsions?

0

1 Answer 1

3

The DTO is not structured correctly. It should read:

public class CreateCadRequestError {
    public CreateCadRequestErrorMessage error;
}
public class CreateCadRequestErrorMessage {
    public string message;
}

The json2apex app can help you properly structure your DTO classes given a sample JSON input.

Not the answer you're looking for? Browse other questions tagged .