class MsoCommonUtils {

    public MsoException keystoneErrorToMsoException(OpenStackBaseException e, String context) {
        MsoException me = null;

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

            try {
                // Failed Keystone calls return an Error entity body.
                Error error = re.getResponse().getErrorEntity(Error.class);
                logger.error("{} {} Openstack Keystone Error on {}: {}", MessageEnum.RA_CONNECTION_EXCEPTION,
                        ErrorCode.DataError.getValue(), context, error);
                me = new MsoOpenstackException(error.getCode(), error.getTitle(), error.getMessage());
            } catch (Exception e2) {
                // Can't parse the body as an "Error". Report the HTTP error
                logger.error("{} {} HTTP Error on {}: {}, {}", MessageEnum.RA_CONNECTION_EXCEPTION,
                        ErrorCode.DataError.getValue(), context, re.getStatus(), re.getMessage(), e2);
                me = new MsoOpenstackException(re.getStatus(), re.getMessage(), "");
            }

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

            // Generate an alarm for 5XX and higher errors.
            if (re.getStatus() >= 500) {
                logger.error("{} {} OpenStackResponseException with response code {} on {}: ",
                        MessageEnum.RA_CONNECTION_EXCEPTION, ErrorCode.DataError.getValue(), re.getStatus(), context,
                        e);
            }
        } 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 Keystone connection error on {}: ", MessageEnum.RA_GENERAL_EXCEPTION_ARG,
                    ErrorCode.DataError.getValue(), context, e);
        }

        return me;
    }

}