@Test public void testMap() { final BackgroundException e = new SDSExceptionMappingService().map(new ApiException("m", 403, Collections.emptyMap(), "{\"errorCode\" = -40761}")); assertEquals("Error -40761. Please contact your web hosting service provider for assistance.", e.getDetail()); }
@Override public BackgroundException map(final ApiException failure) { for(Throwable cause : ExceptionUtils.getThrowableList(failure)) { if(cause instanceof SocketException) { return new DefaultSocketExceptionMappingService().map((SocketException) cause); } if(cause instanceof HttpResponseException) { return new DefaultHttpResponseExceptionMappingService().map((HttpResponseException) cause); } if(cause instanceof IOException) { return new DefaultIOExceptionMappingService().map((IOException) cause); } if(cause instanceof IllegalStateException) { return new ConnectionCanceledException(cause); } } final StringBuilder buffer = new StringBuilder(); if(null != failure.getResponseBody()) { try { final JsonObject json = JsonParser.parseReader(new StringReader(failure.getResponseBody())).getAsJsonObject(); if(json.has("errorCode")) { if(json.get("errorCode").isJsonPrimitive()) { final int errorCode = json.getAsJsonPrimitive("errorCode").getAsInt(); if(log.isDebugEnabled()) { log.debug(String.format("Failure with errorCode %s", errorCode)); } final String key = String.format("Error %d", errorCode); final String localized = LocaleFactory.get().localize(key, "SDS"); this.append(buffer, localized); if(StringUtils.equals(localized, key)) { log.warn(String.format("Missing user message for error code %d", errorCode)); if(json.has("debugInfo")) { if(json.get("debugInfo").isJsonPrimitive()) { this.append(buffer, json.getAsJsonPrimitive("debugInfo").getAsString()); } } } switch(failure.getCode()) { case HttpStatus.SC_BAD_REQUEST: switch(errorCode) { case -10100: return new AccessDeniedException(buffer.toString(), failure); } case HttpStatus.SC_NOT_FOUND: switch(errorCode) { case -70501: return new AccessDeniedException(buffer.toString(), failure); case -40761: return new AccessDeniedException(buffer.toString(), failure); } break; case HttpStatus.SC_PRECONDITION_FAILED: switch(errorCode) { case -10108: if(json.has("replyMessage")) { if(json.get("replyMessage").isJsonPrimitive()) { final JsonPrimitive replyMessage = json.getAsJsonPrimitive("replyMessage"); if(log.isDebugEnabled()) { log.debug(String.format("Failure with replyMessage %s", replyMessage)); } buffer.append(replyMessage.getAsString()); } } return new PartialLoginFailureException(buffer.toString(), failure); } break; case HttpStatus.SC_UNAUTHORIZED: switch(errorCode) { case -10012: return new ExpiredTokenException(buffer.toString(), failure); } break; } } } else { switch(failure.getCode()) { case HttpStatus.SC_INTERNAL_SERVER_ERROR: break; default: if(json.has("debugInfo")) { log.warn(String.format("Missing error code for failure %s", json)); if(json.get("debugInfo").isJsonPrimitive()) { this.append(buffer, json.getAsJsonPrimitive("debugInfo").getAsString()); } } } } } catch(JsonParseException e) { } } switch(failure.getCode()) { case HttpStatus.SC_FORBIDDEN: if(failure.getResponseHeaders().containsKey("X-Forbidden")) { return new AccessDeniedException(LocaleFactory.localizedString("The AV scanner detected that the file could be malicious", "SDS")); } break; case 901: return new AccessDeniedException(LocaleFactory.localizedString("The AV scanner detected that the file could be malicious", "SDS")); case HttpStatus.SC_PRECONDITION_FAILED: return new LoginFailureException(buffer.toString(), failure); } return new DefaultHttpResponseExceptionMappingService().map(failure, buffer, failure.getCode()); }
SDSExceptionMappingService extends AbstractExceptionMappingService<ApiException> { @Override public BackgroundException map(final ApiException failure) { for(Throwable cause : ExceptionUtils.getThrowableList(failure)) { if(cause instanceof SocketException) { return new DefaultSocketExceptionMappingService().map((SocketException) cause); } if(cause instanceof HttpResponseException) { return new DefaultHttpResponseExceptionMappingService().map((HttpResponseException) cause); } if(cause instanceof IOException) { return new DefaultIOExceptionMappingService().map((IOException) cause); } if(cause instanceof IllegalStateException) { return new ConnectionCanceledException(cause); } } final StringBuilder buffer = new StringBuilder(); if(null != failure.getResponseBody()) { try { final JsonObject json = JsonParser.parseReader(new StringReader(failure.getResponseBody())).getAsJsonObject(); if(json.has("errorCode")) { if(json.get("errorCode").isJsonPrimitive()) { final int errorCode = json.getAsJsonPrimitive("errorCode").getAsInt(); if(log.isDebugEnabled()) { log.debug(String.format("Failure with errorCode %s", errorCode)); } final String key = String.format("Error %d", errorCode); final String localized = LocaleFactory.get().localize(key, "SDS"); this.append(buffer, localized); if(StringUtils.equals(localized, key)) { log.warn(String.format("Missing user message for error code %d", errorCode)); if(json.has("debugInfo")) { if(json.get("debugInfo").isJsonPrimitive()) { this.append(buffer, json.getAsJsonPrimitive("debugInfo").getAsString()); } } } switch(failure.getCode()) { case HttpStatus.SC_BAD_REQUEST: switch(errorCode) { case -10100: return new AccessDeniedException(buffer.toString(), failure); } case HttpStatus.SC_NOT_FOUND: switch(errorCode) { case -70501: return new AccessDeniedException(buffer.toString(), failure); case -40761: return new AccessDeniedException(buffer.toString(), failure); } break; case HttpStatus.SC_PRECONDITION_FAILED: switch(errorCode) { case -10108: if(json.has("replyMessage")) { if(json.get("replyMessage").isJsonPrimitive()) { final JsonPrimitive replyMessage = json.getAsJsonPrimitive("replyMessage"); if(log.isDebugEnabled()) { log.debug(String.format("Failure with replyMessage %s", replyMessage)); } buffer.append(replyMessage.getAsString()); } } return new PartialLoginFailureException(buffer.toString(), failure); } break; case HttpStatus.SC_UNAUTHORIZED: switch(errorCode) { case -10012: return new ExpiredTokenException(buffer.toString(), failure); } break; } } } else { switch(failure.getCode()) { case HttpStatus.SC_INTERNAL_SERVER_ERROR: break; default: if(json.has("debugInfo")) { log.warn(String.format("Missing error code for failure %s", json)); if(json.get("debugInfo").isJsonPrimitive()) { this.append(buffer, json.getAsJsonPrimitive("debugInfo").getAsString()); } } } } } catch(JsonParseException e) { } } switch(failure.getCode()) { case HttpStatus.SC_FORBIDDEN: if(failure.getResponseHeaders().containsKey("X-Forbidden")) { return new AccessDeniedException(LocaleFactory.localizedString("The AV scanner detected that the file could be malicious", "SDS")); } break; case 901: return new AccessDeniedException(LocaleFactory.localizedString("The AV scanner detected that the file could be malicious", "SDS")); case HttpStatus.SC_PRECONDITION_FAILED: return new LoginFailureException(buffer.toString(), failure); } return new DefaultHttpResponseExceptionMappingService().map(failure, buffer, failure.getCode()); } }
SDSExceptionMappingService extends AbstractExceptionMappingService<ApiException> { @Override public BackgroundException map(final ApiException failure) { for(Throwable cause : ExceptionUtils.getThrowableList(failure)) { if(cause instanceof SocketException) { return new DefaultSocketExceptionMappingService().map((SocketException) cause); } if(cause instanceof HttpResponseException) { return new DefaultHttpResponseExceptionMappingService().map((HttpResponseException) cause); } if(cause instanceof IOException) { return new DefaultIOExceptionMappingService().map((IOException) cause); } if(cause instanceof IllegalStateException) { return new ConnectionCanceledException(cause); } } final StringBuilder buffer = new StringBuilder(); if(null != failure.getResponseBody()) { try { final JsonObject json = JsonParser.parseReader(new StringReader(failure.getResponseBody())).getAsJsonObject(); if(json.has("errorCode")) { if(json.get("errorCode").isJsonPrimitive()) { final int errorCode = json.getAsJsonPrimitive("errorCode").getAsInt(); if(log.isDebugEnabled()) { log.debug(String.format("Failure with errorCode %s", errorCode)); } final String key = String.format("Error %d", errorCode); final String localized = LocaleFactory.get().localize(key, "SDS"); this.append(buffer, localized); if(StringUtils.equals(localized, key)) { log.warn(String.format("Missing user message for error code %d", errorCode)); if(json.has("debugInfo")) { if(json.get("debugInfo").isJsonPrimitive()) { this.append(buffer, json.getAsJsonPrimitive("debugInfo").getAsString()); } } } switch(failure.getCode()) { case HttpStatus.SC_BAD_REQUEST: switch(errorCode) { case -10100: return new AccessDeniedException(buffer.toString(), failure); } case HttpStatus.SC_NOT_FOUND: switch(errorCode) { case -70501: return new AccessDeniedException(buffer.toString(), failure); case -40761: return new AccessDeniedException(buffer.toString(), failure); } break; case HttpStatus.SC_PRECONDITION_FAILED: switch(errorCode) { case -10108: if(json.has("replyMessage")) { if(json.get("replyMessage").isJsonPrimitive()) { final JsonPrimitive replyMessage = json.getAsJsonPrimitive("replyMessage"); if(log.isDebugEnabled()) { log.debug(String.format("Failure with replyMessage %s", replyMessage)); } buffer.append(replyMessage.getAsString()); } } return new PartialLoginFailureException(buffer.toString(), failure); } break; case HttpStatus.SC_UNAUTHORIZED: switch(errorCode) { case -10012: return new ExpiredTokenException(buffer.toString(), failure); } break; } } } else { switch(failure.getCode()) { case HttpStatus.SC_INTERNAL_SERVER_ERROR: break; default: if(json.has("debugInfo")) { log.warn(String.format("Missing error code for failure %s", json)); if(json.get("debugInfo").isJsonPrimitive()) { this.append(buffer, json.getAsJsonPrimitive("debugInfo").getAsString()); } } } } } catch(JsonParseException e) { } } switch(failure.getCode()) { case HttpStatus.SC_FORBIDDEN: if(failure.getResponseHeaders().containsKey("X-Forbidden")) { return new AccessDeniedException(LocaleFactory.localizedString("The AV scanner detected that the file could be malicious", "SDS")); } break; case 901: return new AccessDeniedException(LocaleFactory.localizedString("The AV scanner detected that the file could be malicious", "SDS")); case HttpStatus.SC_PRECONDITION_FAILED: return new LoginFailureException(buffer.toString(), failure); } return new DefaultHttpResponseExceptionMappingService().map(failure, buffer, failure.getCode()); }  }
SDSExceptionMappingService extends AbstractExceptionMappingService<ApiException> { @Override public BackgroundException map(final ApiException failure) { for(Throwable cause : ExceptionUtils.getThrowableList(failure)) { if(cause instanceof SocketException) { return new DefaultSocketExceptionMappingService().map((SocketException) cause); } if(cause instanceof HttpResponseException) { return new DefaultHttpResponseExceptionMappingService().map((HttpResponseException) cause); } if(cause instanceof IOException) { return new DefaultIOExceptionMappingService().map((IOException) cause); } if(cause instanceof IllegalStateException) { return new ConnectionCanceledException(cause); } } final StringBuilder buffer = new StringBuilder(); if(null != failure.getResponseBody()) { try { final JsonObject json = JsonParser.parseReader(new StringReader(failure.getResponseBody())).getAsJsonObject(); if(json.has("errorCode")) { if(json.get("errorCode").isJsonPrimitive()) { final int errorCode = json.getAsJsonPrimitive("errorCode").getAsInt(); if(log.isDebugEnabled()) { log.debug(String.format("Failure with errorCode %s", errorCode)); } final String key = String.format("Error %d", errorCode); final String localized = LocaleFactory.get().localize(key, "SDS"); this.append(buffer, localized); if(StringUtils.equals(localized, key)) { log.warn(String.format("Missing user message for error code %d", errorCode)); if(json.has("debugInfo")) { if(json.get("debugInfo").isJsonPrimitive()) { this.append(buffer, json.getAsJsonPrimitive("debugInfo").getAsString()); } } } switch(failure.getCode()) { case HttpStatus.SC_BAD_REQUEST: switch(errorCode) { case -10100: return new AccessDeniedException(buffer.toString(), failure); } case HttpStatus.SC_NOT_FOUND: switch(errorCode) { case -70501: return new AccessDeniedException(buffer.toString(), failure); case -40761: return new AccessDeniedException(buffer.toString(), failure); } break; case HttpStatus.SC_PRECONDITION_FAILED: switch(errorCode) { case -10108: if(json.has("replyMessage")) { if(json.get("replyMessage").isJsonPrimitive()) { final JsonPrimitive replyMessage = json.getAsJsonPrimitive("replyMessage"); if(log.isDebugEnabled()) { log.debug(String.format("Failure with replyMessage %s", replyMessage)); } buffer.append(replyMessage.getAsString()); } } return new PartialLoginFailureException(buffer.toString(), failure); } break; case HttpStatus.SC_UNAUTHORIZED: switch(errorCode) { case -10012: return new ExpiredTokenException(buffer.toString(), failure); } break; } } } else { switch(failure.getCode()) { case HttpStatus.SC_INTERNAL_SERVER_ERROR: break; default: if(json.has("debugInfo")) { log.warn(String.format("Missing error code for failure %s", json)); if(json.get("debugInfo").isJsonPrimitive()) { this.append(buffer, json.getAsJsonPrimitive("debugInfo").getAsString()); } } } } } catch(JsonParseException e) { } } switch(failure.getCode()) { case HttpStatus.SC_FORBIDDEN: if(failure.getResponseHeaders().containsKey("X-Forbidden")) { return new AccessDeniedException(LocaleFactory.localizedString("The AV scanner detected that the file could be malicious", "SDS")); } break; case 901: return new AccessDeniedException(LocaleFactory.localizedString("The AV scanner detected that the file could be malicious", "SDS")); case HttpStatus.SC_PRECONDITION_FAILED: return new LoginFailureException(buffer.toString(), failure); } return new DefaultHttpResponseExceptionMappingService().map(failure, buffer, failure.getCode()); }  @Override BackgroundException map(final ApiException failure); }
SDSExceptionMappingService extends AbstractExceptionMappingService<ApiException> { @Override public BackgroundException map(final ApiException failure) { for(Throwable cause : ExceptionUtils.getThrowableList(failure)) { if(cause instanceof SocketException) { return new DefaultSocketExceptionMappingService().map((SocketException) cause); } if(cause instanceof HttpResponseException) { return new DefaultHttpResponseExceptionMappingService().map((HttpResponseException) cause); } if(cause instanceof IOException) { return new DefaultIOExceptionMappingService().map((IOException) cause); } if(cause instanceof IllegalStateException) { return new ConnectionCanceledException(cause); } } final StringBuilder buffer = new StringBuilder(); if(null != failure.getResponseBody()) { try { final JsonObject json = JsonParser.parseReader(new StringReader(failure.getResponseBody())).getAsJsonObject(); if(json.has("errorCode")) { if(json.get("errorCode").isJsonPrimitive()) { final int errorCode = json.getAsJsonPrimitive("errorCode").getAsInt(); if(log.isDebugEnabled()) { log.debug(String.format("Failure with errorCode %s", errorCode)); } final String key = String.format("Error %d", errorCode); final String localized = LocaleFactory.get().localize(key, "SDS"); this.append(buffer, localized); if(StringUtils.equals(localized, key)) { log.warn(String.format("Missing user message for error code %d", errorCode)); if(json.has("debugInfo")) { if(json.get("debugInfo").isJsonPrimitive()) { this.append(buffer, json.getAsJsonPrimitive("debugInfo").getAsString()); } } } switch(failure.getCode()) { case HttpStatus.SC_BAD_REQUEST: switch(errorCode) { case -10100: return new AccessDeniedException(buffer.toString(), failure); } case HttpStatus.SC_NOT_FOUND: switch(errorCode) { case -70501: return new AccessDeniedException(buffer.toString(), failure); case -40761: return new AccessDeniedException(buffer.toString(), failure); } break; case HttpStatus.SC_PRECONDITION_FAILED: switch(errorCode) { case -10108: if(json.has("replyMessage")) { if(json.get("replyMessage").isJsonPrimitive()) { final JsonPrimitive replyMessage = json.getAsJsonPrimitive("replyMessage"); if(log.isDebugEnabled()) { log.debug(String.format("Failure with replyMessage %s", replyMessage)); } buffer.append(replyMessage.getAsString()); } } return new PartialLoginFailureException(buffer.toString(), failure); } break; case HttpStatus.SC_UNAUTHORIZED: switch(errorCode) { case -10012: return new ExpiredTokenException(buffer.toString(), failure); } break; } } } else { switch(failure.getCode()) { case HttpStatus.SC_INTERNAL_SERVER_ERROR: break; default: if(json.has("debugInfo")) { log.warn(String.format("Missing error code for failure %s", json)); if(json.get("debugInfo").isJsonPrimitive()) { this.append(buffer, json.getAsJsonPrimitive("debugInfo").getAsString()); } } } } } catch(JsonParseException e) { } } switch(failure.getCode()) { case HttpStatus.SC_FORBIDDEN: if(failure.getResponseHeaders().containsKey("X-Forbidden")) { return new AccessDeniedException(LocaleFactory.localizedString("The AV scanner detected that the file could be malicious", "SDS")); } break; case 901: return new AccessDeniedException(LocaleFactory.localizedString("The AV scanner detected that the file could be malicious", "SDS")); case HttpStatus.SC_PRECONDITION_FAILED: return new LoginFailureException(buffer.toString(), failure); } return new DefaultHttpResponseExceptionMappingService().map(failure, buffer, failure.getCode()); }  @Override BackgroundException map(final ApiException failure);  }