Search in sources :

Example 1 with SMPClientBadRequestException

use of com.helger.smpclient.exception.SMPClientBadRequestException in project peppol-commons by phax.

the class AbstractGenericSMPClient method getConvertedException.

/**
 * Convert the passed generic HTTP exception into a more specific exception.
 *
 * @param ex
 *        The generic exception. May not be <code>null</code>.
 * @return A new SMP specific exception, using the passed exception as the
 *         cause.
 */
@Nonnull
public static SMPClientException getConvertedException(@Nonnull final Exception ex) {
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("Converting exception of class '" + ex.getClass().getName() + "' to an SMP expception");
    if (ex instanceof SMPClientException)
        return (SMPClientException) ex;
    if (ex instanceof HttpResponseException) {
        final HttpResponseException hex = (HttpResponseException) ex;
        final int nHttpStatus = hex.getStatusCode();
        switch(nHttpStatus) {
            case HttpStatus.SC_BAD_REQUEST:
                return new SMPClientBadRequestException(hex);
            case HttpStatus.SC_FORBIDDEN:
                return new SMPClientUnauthorizedException(hex);
            case HttpStatus.SC_NOT_FOUND:
                return new SMPClientNotFoundException(hex);
            default:
                return new SMPClientException("Error thrown with HTTP status code " + nHttpStatus, hex);
        }
    }
    // Special case
    if (ex instanceof UnknownHostException)
        return new SMPClientNotFoundException((UnknownHostException) ex);
    if (ex instanceof ConnectException)
        return new SMPClientNotFoundException((ConnectException) ex);
    // For new SMPClientBadResponseException
    if (ex instanceof ClientProtocolException && ex.getCause() instanceof SMPClientException)
        return (SMPClientException) ex.getCause();
    // Generic version
    return new SMPClientException("Unknown error thrown by SMP server (" + ex.getMessage() + ")", ex);
}
Also used : SMPClientNotFoundException(com.helger.smpclient.exception.SMPClientNotFoundException) UnknownHostException(java.net.UnknownHostException) SMPClientException(com.helger.smpclient.exception.SMPClientException) SMPClientBadRequestException(com.helger.smpclient.exception.SMPClientBadRequestException) HttpResponseException(org.apache.http.client.HttpResponseException) SMPClientUnauthorizedException(com.helger.smpclient.exception.SMPClientUnauthorizedException) ConnectException(java.net.ConnectException) ClientProtocolException(org.apache.http.client.ClientProtocolException) Nonnull(javax.annotation.Nonnull)

Aggregations

SMPClientBadRequestException (com.helger.smpclient.exception.SMPClientBadRequestException)1 SMPClientException (com.helger.smpclient.exception.SMPClientException)1 SMPClientNotFoundException (com.helger.smpclient.exception.SMPClientNotFoundException)1 SMPClientUnauthorizedException (com.helger.smpclient.exception.SMPClientUnauthorizedException)1 ConnectException (java.net.ConnectException)1 UnknownHostException (java.net.UnknownHostException)1 Nonnull (javax.annotation.Nonnull)1 ClientProtocolException (org.apache.http.client.ClientProtocolException)1 HttpResponseException (org.apache.http.client.HttpResponseException)1