From b8b86c236fe128f2724547db8d4e12384d4e79c2 Mon Sep 17 00:00:00 2001 From: Fabian Engelniederhammer Date: Mon, 16 Oct 2023 12:57:23 +0200 Subject: [PATCH] feat: throw more specialized exception to handle bad requests We don't know where IllegalArgumentExceptions are thrown in library code and whether this is a "Bad Request" for us. --- .../lapis/controller/ExceptionHandler.kt | 8 ++-- .../lapis/model/SiloFilterExpressionMapper.kt | 27 +++++++------- .../lapis/request/AminoAcidInsertion.kt | 9 +++-- .../lapis/request/AminoAcidMutation.kt | 7 ++-- .../lapis/request/CommonSequenceFilters.kt | 15 ++++---- .../request/MutationProportionsRequest.kt | 3 +- .../lapis/request/NucleotideInsertion.kt | 7 ++-- .../lapis/request/NucleotideMutation.kt | 5 ++- .../genspectrum/lapis/request/OrderByField.kt | 7 ++-- .../SequenceFiltersRequestWithFields.kt | 3 +- .../lapis/controller/ExceptionHandlerTest.kt | 2 +- .../model/SiloFilterExpressionMapperTest.kt | 37 ++++++++++--------- .../lapis/request/AminoAcidInsertionTest.kt | 4 +- .../lapis/request/AminoAcidMutationTest.kt | 3 +- .../request/MutationProportionsRequestTest.kt | 3 +- .../lapis/request/NucleotideInsertionTest.kt | 4 +- .../lapis/request/NucleotideMutationTest.kt | 3 +- .../SequenceFiltersRequestWithFieldsTest.kt | 3 +- 18 files changed, 82 insertions(+), 68 deletions(-) diff --git a/lapis2/src/main/kotlin/org/genspectrum/lapis/controller/ExceptionHandler.kt b/lapis2/src/main/kotlin/org/genspectrum/lapis/controller/ExceptionHandler.kt index 55e85efc..f3a412ca 100644 --- a/lapis2/src/main/kotlin/org/genspectrum/lapis/controller/ExceptionHandler.kt +++ b/lapis2/src/main/kotlin/org/genspectrum/lapis/controller/ExceptionHandler.kt @@ -27,10 +27,10 @@ class ExceptionHandler : ResponseEntityExceptionHandler() { return responseEntity(HttpStatus.INTERNAL_SERVER_ERROR, e.message) } - @ExceptionHandler(IllegalArgumentException::class) + @ExceptionHandler(BadRequestException::class) @ResponseStatus(HttpStatus.BAD_REQUEST) - fun handleIllegalArgumentException(e: IllegalArgumentException): ErrorResponse { - log.warn(e) { "Caught IllegalArgumentException: ${e.message}" } + fun handleBadRequestException(e: BadRequestException): ErrorResponse { + log.warn(e) { "Caught BadRequestException: ${e.message}" } return responseEntity(HttpStatus.BAD_REQUEST, e.message) } @@ -83,3 +83,5 @@ class ExceptionHandler : ResponseEntityExceptionHandler() { /** This is not yet actually thrown, but makes "403 Forbidden" appear in OpenAPI docs. */ class AddForbiddenToOpenApiDocsHelper(message: String) : Exception(message) + +class BadRequestException(message: String, cause: Throwable? = null) : Exception(message, cause) diff --git a/lapis2/src/main/kotlin/org/genspectrum/lapis/model/SiloFilterExpressionMapper.kt b/lapis2/src/main/kotlin/org/genspectrum/lapis/model/SiloFilterExpressionMapper.kt index efe23853..c834334d 100644 --- a/lapis2/src/main/kotlin/org/genspectrum/lapis/model/SiloFilterExpressionMapper.kt +++ b/lapis2/src/main/kotlin/org/genspectrum/lapis/model/SiloFilterExpressionMapper.kt @@ -2,6 +2,7 @@ package org.genspectrum.lapis.model import org.genspectrum.lapis.config.SequenceFilterFieldType import org.genspectrum.lapis.config.SequenceFilterFields +import org.genspectrum.lapis.controller.BadRequestException import org.genspectrum.lapis.request.AminoAcidInsertion import org.genspectrum.lapis.request.AminoAcidMutation import org.genspectrum.lapis.request.CommonSequenceFilters @@ -103,7 +104,7 @@ class SiloFilterExpressionMapper( is SequenceFilterFieldType.FloatFrom -> Pair(type.associatedField, Filter.FloatBetween) is SequenceFilterFieldType.FloatTo -> Pair(type.associatedField, Filter.FloatBetween) - null -> throw IllegalArgumentException( + null -> throw BadRequestException( "'$key' is not a valid sequence filter key. Valid keys are: " + allowedSequenceFilterFields.fields.keys.joinToString(), ) @@ -122,7 +123,7 @@ class SiloFilterExpressionMapper( aaMutations.isNotEmpty() if (containsAdvancedVariantQuery && containsSimpleVariantQuery) { - throw IllegalArgumentException( + throw BadRequestException( "variantQuery filter cannot be used with other variant filters such as: " + variantQueryTypes.joinToString(", "), ) @@ -135,7 +136,7 @@ class SiloFilterExpressionMapper( ] if (intBetweenFilterForSameColumn != null) { - throw IllegalArgumentException( + throw BadRequestException( "Cannot filter by exact int field '$intEqualsColumnName' " + "and by int range field '${intBetweenFilterForSameColumn[0].originalKey}'.", ) @@ -149,7 +150,7 @@ class SiloFilterExpressionMapper( ] if (floatBetweenFilterForSameColumn != null) { - throw IllegalArgumentException( + throw BadRequestException( "Cannot filter by exact float field '$floatEqualsColumnName' " + "and by float range field '${floatBetweenFilterForSameColumn[0].originalKey}'.", ) @@ -159,9 +160,7 @@ class SiloFilterExpressionMapper( private fun mapToVariantQueryFilter(variantQuery: String): SiloFilterExpression { if (variantQuery.isBlank()) { - throw IllegalArgumentException( - "variantQuery must not be empty", - ) + throw BadRequestException("variantQuery must not be empty") } return variantQueryFacade.map(variantQuery) @@ -176,7 +175,7 @@ class SiloFilterExpressionMapper( } if (exactDateFilters.isNotEmpty() && dateRangeFilters.isNotEmpty()) { - throw IllegalArgumentException( + throw BadRequestException( "Cannot filter by exact date field '${exactDateFilters[0].originalKey}' " + "and by date range field '${dateRangeFilters[0].originalKey}'.", ) @@ -211,14 +210,14 @@ class SiloFilterExpressionMapper( try { return LocalDate.parse(value) } catch (exception: DateTimeParseException) { - throw IllegalArgumentException("$originalKey '$value' is not a valid date: ${exception.message}", exception) + throw BadRequestException("$originalKey '$value' is not a valid date: ${exception.message}", exception) } } private fun mapToPangoLineageFilter(column: String, value: String) = when { value.endsWith(".*") -> PangoLineageEquals(column, value.substringBeforeLast(".*"), includeSublineages = true) value.endsWith('*') -> PangoLineageEquals(column, value.substringBeforeLast('*'), includeSublineages = true) - value.endsWith('.') -> throw IllegalArgumentException( + value.endsWith('.') -> throw BadRequestException( "Invalid pango lineage: $value must not end with a dot. Did you mean '$value*'?", ) @@ -233,7 +232,7 @@ class SiloFilterExpressionMapper( try { return IntEquals(siloColumnName, value.toInt()) } catch (exception: NumberFormatException) { - throw IllegalArgumentException( + throw BadRequestException( "$siloColumnName '$value' is not a valid integer: ${exception.message}", exception, ) @@ -248,7 +247,7 @@ class SiloFilterExpressionMapper( try { return FloatEquals(siloColumnName, value.toDouble()) } catch (exception: NumberFormatException) { - throw IllegalArgumentException( + throw BadRequestException( "$siloColumnName '$value' is not a valid float: ${exception.message}", exception, ) @@ -274,7 +273,7 @@ class SiloFilterExpressionMapper( try { return value.toInt() } catch (exception: NumberFormatException) { - throw IllegalArgumentException( + throw BadRequestException( "$originalKey '$value' is not a valid integer: ${exception.message}", exception, ) @@ -300,7 +299,7 @@ class SiloFilterExpressionMapper( try { return value.toDouble() } catch (exception: NumberFormatException) { - throw IllegalArgumentException( + throw BadRequestException( "$originalKey '$value' is not a valid float: ${exception.message}", exception, ) diff --git a/lapis2/src/main/kotlin/org/genspectrum/lapis/request/AminoAcidInsertion.kt b/lapis2/src/main/kotlin/org/genspectrum/lapis/request/AminoAcidInsertion.kt index ea7e06be..2f6978c9 100644 --- a/lapis2/src/main/kotlin/org/genspectrum/lapis/request/AminoAcidInsertion.kt +++ b/lapis2/src/main/kotlin/org/genspectrum/lapis/request/AminoAcidInsertion.kt @@ -3,6 +3,7 @@ package org.genspectrum.lapis.request import com.fasterxml.jackson.core.JsonParser import com.fasterxml.jackson.databind.DeserializationContext import com.fasterxml.jackson.databind.JsonDeserializer +import org.genspectrum.lapis.controller.BadRequestException import org.springframework.boot.jackson.JsonComponent import org.springframework.core.convert.converter.Converter import org.springframework.stereotype.Component @@ -11,22 +12,22 @@ data class AminoAcidInsertion(val position: Int, val gene: String, val insertion companion object { fun fromString(aminoAcidInsertion: String): AminoAcidInsertion { val match = AMINO_ACID_INSERTION_REGEX.find(aminoAcidInsertion) - ?: throw IllegalArgumentException("Invalid nucleotide mutation: $aminoAcidInsertion") + ?: throw BadRequestException("Invalid nucleotide mutation: $aminoAcidInsertion") val matchGroups = match.groups val position = matchGroups["position"]?.value?.toInt() - ?: throw IllegalArgumentException( + ?: throw BadRequestException( "Invalid amino acid insertion: $aminoAcidInsertion: Did not find position", ) val gene = matchGroups["gene"]?.value - ?: throw IllegalArgumentException( + ?: throw BadRequestException( "Invalid amino acid insertion: $aminoAcidInsertion: Did not find gene", ) val insertions = matchGroups["insertions"]?.value?.replace("?", ".*") - ?: throw IllegalArgumentException( + ?: throw BadRequestException( "Invalid amino acid insertion: $aminoAcidInsertion: Did not find insertions", ) diff --git a/lapis2/src/main/kotlin/org/genspectrum/lapis/request/AminoAcidMutation.kt b/lapis2/src/main/kotlin/org/genspectrum/lapis/request/AminoAcidMutation.kt index 4a0ad94f..dbfd9e0b 100644 --- a/lapis2/src/main/kotlin/org/genspectrum/lapis/request/AminoAcidMutation.kt +++ b/lapis2/src/main/kotlin/org/genspectrum/lapis/request/AminoAcidMutation.kt @@ -3,6 +3,7 @@ package org.genspectrum.lapis.request import com.fasterxml.jackson.core.JsonParser import com.fasterxml.jackson.databind.DeserializationContext import com.fasterxml.jackson.databind.JsonDeserializer +import org.genspectrum.lapis.controller.BadRequestException import org.springframework.boot.jackson.JsonComponent import org.springframework.core.convert.converter.Converter import org.springframework.stereotype.Component @@ -11,14 +12,14 @@ data class AminoAcidMutation(val gene: String, val position: Int, val symbol: St companion object { fun fromString(aminoAcidMutation: String): AminoAcidMutation { val match = AMINO_ACID_MUTATION_REGEX.find(aminoAcidMutation) - ?: throw IllegalArgumentException("Invalid amino acid mutation: $aminoAcidMutation") + ?: throw BadRequestException("Invalid amino acid mutation: $aminoAcidMutation") val matchGroups = match.groups val gene = matchGroups["gene"]?.value - ?: throw IllegalArgumentException("Invalid amino acid mutation: $aminoAcidMutation: Did not find gene") + ?: throw BadRequestException("Invalid amino acid mutation: $aminoAcidMutation: Did not find gene") val position = matchGroups["position"]?.value?.toInt() - ?: throw IllegalArgumentException( + ?: throw BadRequestException( "Invalid amino acid mutation: $aminoAcidMutation: Did not find position", ) diff --git a/lapis2/src/main/kotlin/org/genspectrum/lapis/request/CommonSequenceFilters.kt b/lapis2/src/main/kotlin/org/genspectrum/lapis/request/CommonSequenceFilters.kt index 4b767c9e..079cd266 100644 --- a/lapis2/src/main/kotlin/org/genspectrum/lapis/request/CommonSequenceFilters.kt +++ b/lapis2/src/main/kotlin/org/genspectrum/lapis/request/CommonSequenceFilters.kt @@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.node.ArrayNode import com.fasterxml.jackson.databind.node.JsonNodeType import org.genspectrum.lapis.controller.AMINO_ACID_INSERTIONS_PROPERTY import org.genspectrum.lapis.controller.AMINO_ACID_MUTATIONS_PROPERTY +import org.genspectrum.lapis.controller.BadRequestException import org.genspectrum.lapis.controller.LIMIT_PROPERTY import org.genspectrum.lapis.controller.NUCLEOTIDE_INSERTIONS_PROPERTY import org.genspectrum.lapis.controller.NUCLEOTIDE_MUTATIONS_PROPERTY @@ -32,7 +33,7 @@ fun parseCommonFields(node: JsonNode, codec: ObjectCodec): ParsedCommonFields { val nucleotideMutations = when (val nucleotideMutationsNode = node.get(NUCLEOTIDE_MUTATIONS_PROPERTY)) { null -> emptyList() is ArrayNode -> nucleotideMutationsNode.map { codec.treeToValue(it, NucleotideMutation::class.java) } - else -> throw IllegalArgumentException( + else -> throw BadRequestException( "nucleotideMutations must be an array or null", ) } @@ -40,7 +41,7 @@ fun parseCommonFields(node: JsonNode, codec: ObjectCodec): ParsedCommonFields { val aminoAcidMutations = when (val aminoAcidMutationsNode = node.get(AMINO_ACID_MUTATIONS_PROPERTY)) { null -> emptyList() is ArrayNode -> aminoAcidMutationsNode.map { codec.treeToValue(it, AminoAcidMutation::class.java) } - else -> throw IllegalArgumentException( + else -> throw BadRequestException( "aminoAcidMutations must be an array or null", ) } @@ -48,7 +49,7 @@ fun parseCommonFields(node: JsonNode, codec: ObjectCodec): ParsedCommonFields { val nucleotideInsertions = when (val nucleotideInsertionsNode = node.get(NUCLEOTIDE_INSERTIONS_PROPERTY)) { null -> emptyList() is ArrayNode -> nucleotideInsertionsNode.map { codec.treeToValue(it, NucleotideInsertion::class.java) } - else -> throw IllegalArgumentException( + else -> throw BadRequestException( "nucleotideInsertions must be an array or null", ) } @@ -56,7 +57,7 @@ fun parseCommonFields(node: JsonNode, codec: ObjectCodec): ParsedCommonFields { val aminoAcidInsertions = when (val aminoAcidInsertionsNode = node.get(AMINO_ACID_INSERTIONS_PROPERTY)) { null -> emptyList() is ArrayNode -> aminoAcidInsertionsNode.map { codec.treeToValue(it, AminoAcidInsertion::class.java) } - else -> throw IllegalArgumentException( + else -> throw BadRequestException( "aminoAcidInsertions must be an array or null", ) } @@ -64,7 +65,7 @@ fun parseCommonFields(node: JsonNode, codec: ObjectCodec): ParsedCommonFields { val orderByFields = when (val orderByNode = node.get(ORDER_BY_PROPERTY)) { null -> emptyList() is ArrayNode -> orderByNode.map { codec.treeToValue(it, OrderByField::class.java) } - else -> throw IllegalArgumentException( + else -> throw BadRequestException( "orderBy must be an array or null", ) } @@ -73,14 +74,14 @@ fun parseCommonFields(node: JsonNode, codec: ObjectCodec): ParsedCommonFields { val limit = when (limitNode?.nodeType) { null -> null JsonNodeType.NULL, JsonNodeType.NUMBER -> limitNode.asInt() - else -> throw IllegalArgumentException("limit must be a number or null") + else -> throw BadRequestException("limit must be a number or null") } val offsetNode = node.get(OFFSET_PROPERTY) val offset = when (offsetNode?.nodeType) { null -> null JsonNodeType.NULL, JsonNodeType.NUMBER -> offsetNode.asInt() - else -> throw IllegalArgumentException("offset must be a number or null") + else -> throw BadRequestException("offset must be a number or null") } val sequenceFilters = node.fields().asSequence().filter { isStringOrNumber(it.value) } diff --git a/lapis2/src/main/kotlin/org/genspectrum/lapis/request/MutationProportionsRequest.kt b/lapis2/src/main/kotlin/org/genspectrum/lapis/request/MutationProportionsRequest.kt index b55cdad8..8de5a28f 100644 --- a/lapis2/src/main/kotlin/org/genspectrum/lapis/request/MutationProportionsRequest.kt +++ b/lapis2/src/main/kotlin/org/genspectrum/lapis/request/MutationProportionsRequest.kt @@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.JsonDeserializer import com.fasterxml.jackson.databind.JsonNode import com.fasterxml.jackson.databind.node.NullNode import com.fasterxml.jackson.databind.node.NumericNode +import org.genspectrum.lapis.controller.BadRequestException import org.genspectrum.lapis.controller.MIN_PROPORTION_PROPERTY import org.springframework.boot.jackson.JsonComponent @@ -31,7 +32,7 @@ class MutationProportionsRequestDeserializer : JsonDeserializer null is NumericNode -> minProportionNode.doubleValue() - else -> throw IllegalArgumentException("minProportion must be a number") + else -> throw BadRequestException("minProportion must be a number") } val parsedCommonFields = parseCommonFields(node, codec) diff --git a/lapis2/src/main/kotlin/org/genspectrum/lapis/request/NucleotideInsertion.kt b/lapis2/src/main/kotlin/org/genspectrum/lapis/request/NucleotideInsertion.kt index 532f00ca..4bf28879 100644 --- a/lapis2/src/main/kotlin/org/genspectrum/lapis/request/NucleotideInsertion.kt +++ b/lapis2/src/main/kotlin/org/genspectrum/lapis/request/NucleotideInsertion.kt @@ -3,6 +3,7 @@ package org.genspectrum.lapis.request import com.fasterxml.jackson.core.JsonParser import com.fasterxml.jackson.databind.DeserializationContext import com.fasterxml.jackson.databind.JsonDeserializer +import org.genspectrum.lapis.controller.BadRequestException import org.springframework.boot.jackson.JsonComponent import org.springframework.core.convert.converter.Converter import org.springframework.stereotype.Component @@ -11,17 +12,17 @@ data class NucleotideInsertion(val position: Int, val insertions: String, val se companion object { fun fromString(nucleotideInsertion: String): NucleotideInsertion { val match = NUCLEOTIDE_INSERTION_REGEX.find(nucleotideInsertion) - ?: throw IllegalArgumentException("Invalid nucleotide mutation: $nucleotideInsertion") + ?: throw BadRequestException("Invalid nucleotide mutation: $nucleotideInsertion") val matchGroups = match.groups val position = matchGroups["position"]?.value?.toInt() - ?: throw IllegalArgumentException( + ?: throw BadRequestException( "Invalid nucleotide insertion: $nucleotideInsertion: Did not find position", ) val insertions = matchGroups["insertions"]?.value?.replace("?", ".*") - ?: throw IllegalArgumentException( + ?: throw BadRequestException( "Invalid nucleotide insertion: $nucleotideInsertion: Did not find insertions", ) diff --git a/lapis2/src/main/kotlin/org/genspectrum/lapis/request/NucleotideMutation.kt b/lapis2/src/main/kotlin/org/genspectrum/lapis/request/NucleotideMutation.kt index 5dbda660..c0ef1380 100644 --- a/lapis2/src/main/kotlin/org/genspectrum/lapis/request/NucleotideMutation.kt +++ b/lapis2/src/main/kotlin/org/genspectrum/lapis/request/NucleotideMutation.kt @@ -3,6 +3,7 @@ package org.genspectrum.lapis.request import com.fasterxml.jackson.core.JsonParser import com.fasterxml.jackson.databind.DeserializationContext import com.fasterxml.jackson.databind.JsonDeserializer +import org.genspectrum.lapis.controller.BadRequestException import org.springframework.boot.jackson.JsonComponent import org.springframework.core.convert.converter.Converter import org.springframework.stereotype.Component @@ -11,12 +12,12 @@ data class NucleotideMutation(val sequenceName: String?, val position: Int, val companion object { fun fromString(nucleotideMutation: String): NucleotideMutation { val match = NUCLEOTIDE_MUTATION_REGEX.find(nucleotideMutation) - ?: throw IllegalArgumentException("Invalid nucleotide mutation: $nucleotideMutation") + ?: throw BadRequestException("Invalid nucleotide mutation: $nucleotideMutation") val matchGroups = match.groups val position = matchGroups["position"]?.value?.toInt() - ?: throw IllegalArgumentException( + ?: throw BadRequestException( "Invalid nucleotide mutation: $nucleotideMutation: Did not find position", ) diff --git a/lapis2/src/main/kotlin/org/genspectrum/lapis/request/OrderByField.kt b/lapis2/src/main/kotlin/org/genspectrum/lapis/request/OrderByField.kt index e66145f1..420873d3 100644 --- a/lapis2/src/main/kotlin/org/genspectrum/lapis/request/OrderByField.kt +++ b/lapis2/src/main/kotlin/org/genspectrum/lapis/request/OrderByField.kt @@ -8,6 +8,7 @@ import com.fasterxml.jackson.databind.JsonDeserializer import com.fasterxml.jackson.databind.JsonNode import com.fasterxml.jackson.databind.node.ObjectNode import com.fasterxml.jackson.databind.node.TextNode +import org.genspectrum.lapis.controller.BadRequestException import org.springframework.boot.jackson.JsonComponent import org.springframework.core.convert.converter.Converter import org.springframework.stereotype.Component @@ -32,20 +33,20 @@ class OrderByFieldDeserializer : JsonDeserializer() { return when (val value = jsonParser.readValueAsTree()) { is TextNode -> OrderByField(value.asText(), Order.ASCENDING) is ObjectNode -> deserializeOrderByField(value) - else -> throw IllegalArgumentException("orderByField must be a string or an object") + else -> throw BadRequestException("orderByField must be a string or an object") } } private fun deserializeOrderByField(value: ObjectNode): OrderByField { val fieldNode = value.get("field") if (fieldNode == null || fieldNode !is TextNode) { - throw IllegalArgumentException("orderByField must have a string property \"field\"") + throw BadRequestException("orderByField must have a string property \"field\"") } val ascending = when (value.get("type")?.asText()) { "ascending", null -> Order.ASCENDING "descending" -> Order.DESCENDING - else -> throw IllegalArgumentException("orderByField type must be \"ascending\" or \"descending\"") + else -> throw BadRequestException("orderByField type must be \"ascending\" or \"descending\"") } return OrderByField(fieldNode.asText(), ascending) diff --git a/lapis2/src/main/kotlin/org/genspectrum/lapis/request/SequenceFiltersRequestWithFields.kt b/lapis2/src/main/kotlin/org/genspectrum/lapis/request/SequenceFiltersRequestWithFields.kt index b7127625..5b221581 100644 --- a/lapis2/src/main/kotlin/org/genspectrum/lapis/request/SequenceFiltersRequestWithFields.kt +++ b/lapis2/src/main/kotlin/org/genspectrum/lapis/request/SequenceFiltersRequestWithFields.kt @@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.DeserializationContext import com.fasterxml.jackson.databind.JsonDeserializer import com.fasterxml.jackson.databind.JsonNode import com.fasterxml.jackson.databind.node.ArrayNode +import org.genspectrum.lapis.controller.BadRequestException import org.genspectrum.lapis.controller.FIELDS_PROPERTY import org.springframework.boot.jackson.JsonComponent @@ -29,7 +30,7 @@ class SequenceFiltersRequestWithFieldsDeserializer : JsonDeserializer emptyList() is ArrayNode -> fields.asSequence().map { it.asText() }.toList() - else -> throw IllegalArgumentException( + else -> throw BadRequestException( "fields must be an array or null", ) } diff --git a/lapis2/src/test/kotlin/org/genspectrum/lapis/controller/ExceptionHandlerTest.kt b/lapis2/src/test/kotlin/org/genspectrum/lapis/controller/ExceptionHandlerTest.kt index 31768346..d0375a31 100644 --- a/lapis2/src/test/kotlin/org/genspectrum/lapis/controller/ExceptionHandlerTest.kt +++ b/lapis2/src/test/kotlin/org/genspectrum/lapis/controller/ExceptionHandlerTest.kt @@ -87,7 +87,7 @@ class ExceptionHandlerTest(@Autowired val mockMvc: MockMvc) { @Test fun `throw BAD_REQUEST(400) with additional info for bad requests`() { - every { validControllerCall() } throws IllegalArgumentException("SomeMessage") + every { validControllerCall() } throws BadRequestException("SomeMessage") mockMvc.perform(get(validRoute)) .andExpect(status().isBadRequest) diff --git a/lapis2/src/test/kotlin/org/genspectrum/lapis/model/SiloFilterExpressionMapperTest.kt b/lapis2/src/test/kotlin/org/genspectrum/lapis/model/SiloFilterExpressionMapperTest.kt index b9861ac3..17b2bd06 100644 --- a/lapis2/src/test/kotlin/org/genspectrum/lapis/model/SiloFilterExpressionMapperTest.kt +++ b/lapis2/src/test/kotlin/org/genspectrum/lapis/model/SiloFilterExpressionMapperTest.kt @@ -2,6 +2,7 @@ package org.genspectrum.lapis.model import org.genspectrum.lapis.config.SequenceFilterFieldType import org.genspectrum.lapis.config.SequenceFilterFields +import org.genspectrum.lapis.controller.BadRequestException import org.genspectrum.lapis.request.AminoAcidInsertion import org.genspectrum.lapis.request.AminoAcidMutation import org.genspectrum.lapis.request.CommonSequenceFilters @@ -65,7 +66,7 @@ class SiloFilterExpressionMapperTest { fun `given invalid filter key then throws exception`() { val filterParameter = getSequenceFilters(mapOf("invalid query key" to "some value")) - val exception = assertThrows { underTest.map(filterParameter) } + val exception = assertThrows { underTest.map(filterParameter) } assertThat( exception.message, @@ -97,7 +98,7 @@ class SiloFilterExpressionMapperTest { fun `given invalid date then should throw an exception`() { val filterParameter = getSequenceFilters(mapOf("date" to "this is not a date")) - val exception = assertThrows { underTest.map(filterParameter) } + val exception = assertThrows { underTest.map(filterParameter) } assertThat(exception.message, containsString("date 'this is not a date' is not a valid date")) } @@ -105,7 +106,7 @@ class SiloFilterExpressionMapperTest { fun `given invalid dateTo then should throw an exception`() { val filterParameter = getSequenceFilters(mapOf("dateTo" to "this is not a date")) - val exception = assertThrows { underTest.map(filterParameter) } + val exception = assertThrows { underTest.map(filterParameter) } assertThat(exception.message, containsString("dateTo 'this is not a date' is not a valid date")) } @@ -113,7 +114,7 @@ class SiloFilterExpressionMapperTest { fun `given invalid dateFrom then should throw an exception`() { val filterParameter = getSequenceFilters(mapOf("dateFrom" to "this is not a date either")) - val exception = assertThrows { underTest.map(filterParameter) } + val exception = assertThrows { underTest.map(filterParameter) } assertThat(exception.message, containsString("dateFrom 'this is not a date either' is not a valid date")) } @@ -125,7 +126,7 @@ class SiloFilterExpressionMapperTest { "dateFrom" to "2021-06-03", ), ) - val exception = assertThrows { underTest.map(filterParameter) } + val exception = assertThrows { underTest.map(filterParameter) } assertThat( exception.message, containsString("Cannot filter by exact date field 'date' and by date range field 'dateFrom'."), @@ -140,7 +141,7 @@ class SiloFilterExpressionMapperTest { "dateTo" to "2021-06-03", ), ) - val exception = assertThrows { underTest.map(filterParameter) } + val exception = assertThrows { underTest.map(filterParameter) } assertThat( exception.message, containsString("Cannot filter by exact date field 'date' and by date range field 'dateTo'."), @@ -154,7 +155,7 @@ class SiloFilterExpressionMapperTest { "intField" to "not a number", ), ) - val exception = assertThrows { underTest.map(filterParameter) } + val exception = assertThrows { underTest.map(filterParameter) } assertThat( exception.message, containsString("intField 'not a number' is not a valid integer"), @@ -168,7 +169,7 @@ class SiloFilterExpressionMapperTest { "intFieldTo" to "not a number", ), ) - val exception = assertThrows { underTest.map(filterParameter) } + val exception = assertThrows { underTest.map(filterParameter) } assertThat( exception.message, containsString("intFieldTo 'not a number' is not a valid integer"), @@ -182,7 +183,7 @@ class SiloFilterExpressionMapperTest { "floatField" to "not a number", ), ) - val exception = assertThrows { underTest.map(filterParameter) } + val exception = assertThrows { underTest.map(filterParameter) } assertThat( exception.message, containsString("floatField 'not a number' is not a valid float"), @@ -196,7 +197,7 @@ class SiloFilterExpressionMapperTest { "floatFieldTo" to "not a number", ), ) - val exception = assertThrows { underTest.map(filterParameter) } + val exception = assertThrows { underTest.map(filterParameter) } assertThat( exception.message, containsString("floatFieldTo 'not a number' is not a valid float"), @@ -211,7 +212,7 @@ class SiloFilterExpressionMapperTest { "intFieldFrom" to "43", ), ) - val exception = assertThrows { underTest.map(filterParameter) } + val exception = assertThrows { underTest.map(filterParameter) } assertThat( exception.message, containsString("Cannot filter by exact int field 'intField' and by int range field 'intFieldFrom'."), @@ -226,7 +227,7 @@ class SiloFilterExpressionMapperTest { "intField" to "42", ), ) - val exception = assertThrows { underTest.map(filterParameter) } + val exception = assertThrows { underTest.map(filterParameter) } assertThat( exception.message, containsString("Cannot filter by exact int field 'intField' and by int range field 'intFieldTo'."), @@ -241,7 +242,7 @@ class SiloFilterExpressionMapperTest { "floatFieldFrom" to "42.3", ), ) - val exception = assertThrows { underTest.map(filterParameter) } + val exception = assertThrows { underTest.map(filterParameter) } assertThat( exception.message, containsString( @@ -258,7 +259,7 @@ class SiloFilterExpressionMapperTest { "floatField" to "42.1", ), ) - val exception = assertThrows { underTest.map(filterParameter) } + val exception = assertThrows { underTest.map(filterParameter) } assertThat( exception.message, containsString("Cannot filter by exact float field 'floatField' and by float range field 'floatFieldTo'."), @@ -371,7 +372,7 @@ class SiloFilterExpressionMapperTest { fun `given a query with an empty variantQuery then it should throw an error`() { val filterParameter = getSequenceFilters(mapOf("variantQuery" to "")) - val exception = assertThrows { underTest.map(filterParameter) } + val exception = assertThrows { underTest.map(filterParameter) } assertThat(exception.message, containsString("variantQuery must not be empty")) } @@ -385,7 +386,7 @@ class SiloFilterExpressionMapperTest { emptyList(), ) - val exception = assertThrows { underTest.map(filterParameter) } + val exception = assertThrows { underTest.map(filterParameter) } assertThat( exception.message, containsString("variantQuery filter cannot be used with other variant filters such as: "), @@ -402,7 +403,7 @@ class SiloFilterExpressionMapperTest { emptyList(), ) - val exception = assertThrows { underTest.map(filterParameter) } + val exception = assertThrows { underTest.map(filterParameter) } assertThat( exception.message, containsString("variantQuery filter cannot be used with other variant filters such as: "), @@ -418,7 +419,7 @@ class SiloFilterExpressionMapperTest { ), ) - val exception = assertThrows { underTest.map(filterParameter) } + val exception = assertThrows { underTest.map(filterParameter) } assertThat( exception.message, containsString("variantQuery filter cannot be used with other variant filters such as: "), diff --git a/lapis2/src/test/kotlin/org/genspectrum/lapis/request/AminoAcidInsertionTest.kt b/lapis2/src/test/kotlin/org/genspectrum/lapis/request/AminoAcidInsertionTest.kt index b81db221..7fff3877 100644 --- a/lapis2/src/test/kotlin/org/genspectrum/lapis/request/AminoAcidInsertionTest.kt +++ b/lapis2/src/test/kotlin/org/genspectrum/lapis/request/AminoAcidInsertionTest.kt @@ -2,6 +2,7 @@ package org.genspectrum.lapis.request import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.module.kotlin.readValue +import org.genspectrum.lapis.controller.BadRequestException import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.equalTo import org.junit.jupiter.api.Assertions.assertThrows @@ -30,7 +31,7 @@ class AminoAcidInsertionTest { @ParameterizedTest @MethodSource("getAminoAcidInsertionWithWrongSyntax") fun `Given invalid AminoAcidInsertion then should throw an error`(input: String) { - assertThrows(IllegalArgumentException::class.java) { + assertThrows(BadRequestException::class.java) { objectMapper.readValue(input) } } @@ -78,7 +79,6 @@ class AminoAcidInsertionTest { "\"ins_gene:123:AB.*?CD\"", AminoAcidInsertion(123, "gene", "AB.*.*CD"), ), - ) @JvmStatic diff --git a/lapis2/src/test/kotlin/org/genspectrum/lapis/request/AminoAcidMutationTest.kt b/lapis2/src/test/kotlin/org/genspectrum/lapis/request/AminoAcidMutationTest.kt index 93ba6591..a85ef964 100644 --- a/lapis2/src/test/kotlin/org/genspectrum/lapis/request/AminoAcidMutationTest.kt +++ b/lapis2/src/test/kotlin/org/genspectrum/lapis/request/AminoAcidMutationTest.kt @@ -2,6 +2,7 @@ package org.genspectrum.lapis.request import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.module.kotlin.readValue +import org.genspectrum.lapis.controller.BadRequestException import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.equalTo import org.junit.jupiter.api.Assertions.assertThrows @@ -30,7 +31,7 @@ class AminoAcidMutationTest { @ParameterizedTest @MethodSource("getAminoAcidMutationWithWrongSyntax") fun `Given invalid AminoAcidMutation then should throw an error`(input: String) { - assertThrows(IllegalArgumentException::class.java) { + assertThrows(BadRequestException::class.java) { objectMapper.readValue(input) } } diff --git a/lapis2/src/test/kotlin/org/genspectrum/lapis/request/MutationProportionsRequestTest.kt b/lapis2/src/test/kotlin/org/genspectrum/lapis/request/MutationProportionsRequestTest.kt index b625e59f..92ccefb1 100644 --- a/lapis2/src/test/kotlin/org/genspectrum/lapis/request/MutationProportionsRequestTest.kt +++ b/lapis2/src/test/kotlin/org/genspectrum/lapis/request/MutationProportionsRequestTest.kt @@ -2,6 +2,7 @@ package org.genspectrum.lapis.request import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.module.kotlin.readValue +import org.genspectrum.lapis.controller.BadRequestException import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.equalTo import org.junit.jupiter.api.Assertions.assertThrows @@ -33,7 +34,7 @@ class MutationProportionsRequestTest { input: String, expectedErrorMessage: String, ) { - val exception = assertThrows(IllegalArgumentException::class.java) { + val exception = assertThrows(BadRequestException::class.java) { objectMapper.readValue(input) } diff --git a/lapis2/src/test/kotlin/org/genspectrum/lapis/request/NucleotideInsertionTest.kt b/lapis2/src/test/kotlin/org/genspectrum/lapis/request/NucleotideInsertionTest.kt index 8e2199f7..842859a1 100644 --- a/lapis2/src/test/kotlin/org/genspectrum/lapis/request/NucleotideInsertionTest.kt +++ b/lapis2/src/test/kotlin/org/genspectrum/lapis/request/NucleotideInsertionTest.kt @@ -2,6 +2,7 @@ package org.genspectrum.lapis.request import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.module.kotlin.readValue +import org.genspectrum.lapis.controller.BadRequestException import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.equalTo import org.junit.jupiter.api.Assertions.assertThrows @@ -30,7 +31,7 @@ class NucleotideInsertionTest { @ParameterizedTest @MethodSource("getNucleotideInsertionWithWrongSyntax") fun `Given invalid NucleotideInsertion then should throw an error`(input: String) { - assertThrows(IllegalArgumentException::class.java) { + assertThrows(BadRequestException::class.java) { objectMapper.readValue(input) } } @@ -86,7 +87,6 @@ class NucleotideInsertionTest { "\"ins_segment:123:AB.*?CD\"", NucleotideInsertion(123, "AB.*.*CD", "segment"), ), - ) @JvmStatic diff --git a/lapis2/src/test/kotlin/org/genspectrum/lapis/request/NucleotideMutationTest.kt b/lapis2/src/test/kotlin/org/genspectrum/lapis/request/NucleotideMutationTest.kt index 802f8aa6..f3159eef 100644 --- a/lapis2/src/test/kotlin/org/genspectrum/lapis/request/NucleotideMutationTest.kt +++ b/lapis2/src/test/kotlin/org/genspectrum/lapis/request/NucleotideMutationTest.kt @@ -2,6 +2,7 @@ package org.genspectrum.lapis.request import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.module.kotlin.readValue +import org.genspectrum.lapis.controller.BadRequestException import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.equalTo import org.junit.jupiter.api.Assertions.assertThrows @@ -30,7 +31,7 @@ class NucleotideMutationTest { @ParameterizedTest @MethodSource("getNucleotideMutationWithWrongSyntax") fun `Given invalid NucleotideMutation then should throw an error`(input: String) { - assertThrows(IllegalArgumentException::class.java) { + assertThrows(BadRequestException::class.java) { objectMapper.readValue(input) } } diff --git a/lapis2/src/test/kotlin/org/genspectrum/lapis/request/SequenceFiltersRequestWithFieldsTest.kt b/lapis2/src/test/kotlin/org/genspectrum/lapis/request/SequenceFiltersRequestWithFieldsTest.kt index 76e7072f..ca7cc88e 100644 --- a/lapis2/src/test/kotlin/org/genspectrum/lapis/request/SequenceFiltersRequestWithFieldsTest.kt +++ b/lapis2/src/test/kotlin/org/genspectrum/lapis/request/SequenceFiltersRequestWithFieldsTest.kt @@ -2,6 +2,7 @@ package org.genspectrum.lapis.request import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.module.kotlin.readValue +import org.genspectrum.lapis.controller.BadRequestException import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.equalTo import org.junit.jupiter.api.Assertions.assertThrows @@ -33,7 +34,7 @@ class SequenceFiltersRequestWithFieldsTest { input: String, expectedErrorMessage: String, ) { - val exception = assertThrows(IllegalArgumentException::class.java) { + val exception = assertThrows(BadRequestException::class.java) { objectMapper.readValue(input) }