public static void checkRequest(boolean expression, List<String> messages) { if (!expression) { throw BadRequestException.create(messages); } }
@Override public void validate(String value, @Nullable List<String> options) { try { Long.parseLong(value); } catch (NumberFormatException e) { throw BadRequestException.create(format("Value '%s' must be a long.", value)); } } }
@Override public void validate(String value, @Nullable List<String> options) { try { Integer.parseInt(value); } catch (NumberFormatException e) { throw BadRequestException.create(format("Value '%s' must be an integer.", value)); } }
@Override public void validate(String value, @Nullable List<String> options) { try { Double.parseDouble(value); } catch (NumberFormatException e) { throw BadRequestException.create(format("Value '%s' must be an floating point number.", value)); } }
public static void validateProjectPattern(@Nullable String projectPattern) { if (isNullOrEmpty(projectPattern)) { return; } try { Pattern.compile(projectPattern); } catch (PatternSyntaxException e) { throw BadRequestException.create(format("The '%s' parameter must be a valid Java regular expression. '%s' was passed", PARAM_PROJECT_KEY_PATTERN, projectPattern)); } } }
private ProfileImporter getProfileImporter(String importerKey) { for (ProfileImporter importer : importers) { if (StringUtils.equals(importerKey, importer.getKey())) { return importer; } } throw BadRequestException.create("No such importer : " + importerKey); }
static Long parseId(Request request, String paramName) { try { return Long.valueOf(request.mandatoryParam(paramName)); } catch (NumberFormatException badFormat) { throw BadRequestException.create(paramName + " must be a valid long value"); } }
private static BadRequestException createBadRequestException(EmailException emailException) { List<String> messages = Throwables.getCausalChain(emailException) .stream() .map(Throwable::getMessage) .collect(Collectors.toList()); Collections.reverse(messages); return BadRequestException.create(messages); }
@Override public void validate(String value, @Nullable List<String> options) { try { Metric.Level.valueOf(value); } catch (IllegalArgumentException e) { throw BadRequestException.create(format("Value '%s' must be one of \"OK\", \"ERROR\".", value)); } } }
private ProjectAndSnapshot getProjectAndSnapshot(DbSession dbSession, @Nullable String analysisId, @Nullable String projectId, @Nullable String projectKey) { if (!isNullOrEmpty(analysisId)) { return getSnapshotThenProject(dbSession, analysisId); } if (!isNullOrEmpty(projectId) ^ !isNullOrEmpty(projectKey)) { return getProjectThenSnapshot(dbSession, projectId, projectKey); } throw BadRequestException.create(MSG_ONE_PARAMETER_ONLY); }
private void validateOtherTypes(SettingData data, PropertyDefinition definition) { data.values.stream() .map(definition::validate) .filter(result -> !result.isValid()) .findAny() .ifPresent(result -> { throw BadRequestException.create(i18n.message(Locale.ENGLISH, "property.error." + result.getErrorKey(), format("Error when validating setting with key '%s' and value [%s]", data.key, data.values.stream().collect(Collectors.joining(", "))))); }); }
private static Map<String, String> readOneFieldValues(String json, String key) { Type type = new TypeToken<Map<String, String>>() { }.getType(); Gson gson = GsonHelper.create(); try { return gson.fromJson(json, type); } catch (JsonSyntaxException e) { throw BadRequestException.create(String.format("JSON '%s' does not respect expected format for setting '%s'. Ex: {\"field1\":\"value1\", \"field2\":\"value2\"}", json, key)); } }
@Test public void getMessage_return_first_error() { BadRequestException underTest = BadRequestException.create(asList("error1", "error2")); assertThat(underTest.getMessage()).isEqualTo("error1"); }
@Test public void text_error() { BadRequestException exception = BadRequestException.create("error"); assertThat(exception.getMessage()).isEqualTo("error"); }
@Test public void create_exception_from_list() { BadRequestException underTest = BadRequestException.create(asList("error1", "error2")); assertThat(underTest.errors()).containsOnly("error1", "error2"); }
@Test public void create_exception_from_var_args() { BadRequestException underTest = BadRequestException.create("error1", "error2"); assertThat(underTest.errors()).containsOnly("error1", "error2"); }
@Test public void fail_when_creating_exception_with_one_null_element() { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Message cannot be empty"); BadRequestException.create(asList("error", null)); } }
@Test public void fail_when_creating_exception_with_one_empty_element() { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Message cannot be empty"); BadRequestException.create(asList("error", "")); }
@Test public void fail_when_creating_exception_with_empty_list() { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("At least one error message is required"); BadRequestException.create(Collections.emptyList()); }
@Test public void return_400_on_BadRequestException_with_single_message() { Request request = new TestRequest().setPath("api/foo"); DumbResponse response = run(request, newWs("api/foo", a -> a.setHandler((req, resp) -> { throw BadRequestException.create("Bad request !"); }))); assertThat(response.stream().outputAsString()).isEqualTo( "{\"errors\":[{\"msg\":\"Bad request !\"}]}"); assertThat(response.stream().status()).isEqualTo(400); assertThat(response.stream().mediaType()).isEqualTo(MediaTypes.JSON); assertThat(logTester.logs(LoggerLevel.ERROR)).isEmpty(); }