class MsoCommonUtils {

    protected MsoException heatExceptionToMsoException(OpenStackBaseException e, String context) {
        MsoException me = null;

        if (e instanceof OpenStackResponseException) {
            OpenStackResponseException re = (OpenStackResponseException) e;

            try {
                // Failed Heat calls return an Explanation entity body.
                Explanation explanation = re.getResponse().getErrorEntity(Explanation.class);
                logger.error("Exception - Openstack Error on {} : {}", context, explanation);
                String fullError = explanation.getExplanation() + ", error.type=" + explanation.getError().getType()
                        + ", error.message=" + explanation.getError().getMessage();
                logger.error(fullError);
                me = new MsoOpenstackException(explanation.getCode(), explanation.getTitle(), fullError);
            } catch (Exception e2) {
                // Couldn't parse the body as an "Explanation". Report the original HTTP error.
                logger.error("{} {} Exception - HTTP Error on {}: {}, ", MessageEnum.RA_CONNECTION_EXCEPTION,
                        ErrorCode.DataError.getValue(), context, re.getStatus(), e.getMessage(), e2);
                me = new MsoOpenstackException(re.getStatus(), re.getMessage(), re.getMessage());
            }

            // Add the context of the error
            me.addContext(context);

        } else if (e instanceof OpenStackConnectException) {
            OpenStackConnectException ce = (OpenStackConnectException) e;
            me = new MsoIOException(ce.getMessage());
            me.addContext(context);
            // Generate an alarm for all connection errors.
            logger.error("{} {} Openstack Heat connection error on {}: ", MessageEnum.RA_CONNECTION_EXCEPTION,
                    ErrorCode.DataError.getValue(), context, e);
        }
        return me;
    }

}