Catch search errors and validation
This commit is contained in:
parent
bca47c30f6
commit
e1aac9342e
@ -4,7 +4,9 @@ import jakarta.validation.Valid
|
|||||||
import jakarta.validation.constraints.Min
|
import jakarta.validation.constraints.Min
|
||||||
import jakarta.validation.constraints.NotBlank
|
import jakarta.validation.constraints.NotBlank
|
||||||
import jakarta.validation.constraints.Size
|
import jakarta.validation.constraints.Size
|
||||||
|
import org.slf4j.LoggerFactory
|
||||||
import org.springframework.http.ResponseEntity
|
import org.springframework.http.ResponseEntity
|
||||||
|
import org.springframework.util.StopWatch
|
||||||
import org.springframework.web.bind.annotation.PostMapping
|
import org.springframework.web.bind.annotation.PostMapping
|
||||||
import org.springframework.web.bind.annotation.RequestBody
|
import org.springframework.web.bind.annotation.RequestBody
|
||||||
import org.springframework.web.bind.annotation.RequestHeader
|
import org.springframework.web.bind.annotation.RequestHeader
|
||||||
@ -15,6 +17,8 @@ class SearchController(
|
|||||||
private val searchService: SearchService
|
private val searchService: SearchService
|
||||||
) {
|
) {
|
||||||
|
|
||||||
|
private val logger = LoggerFactory.getLogger(javaClass)
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
|
||||||
const val RESULTS_PER_PAGE = 50
|
const val RESULTS_PER_PAGE = 50
|
||||||
@ -101,7 +105,7 @@ class SearchController(
|
|||||||
)
|
)
|
||||||
|
|
||||||
data class SearchRequest(
|
data class SearchRequest(
|
||||||
@Valid @field:ValidChildQueriesDepth val queries: List<SearchQuery>,
|
@Valid @field:ValidChildQueriesDepth @field:Size(max = 10) val queries: List<SearchQuery>,
|
||||||
@Valid val sorting: SearchSorting,
|
@Valid val sorting: SearchSorting,
|
||||||
@field:Min(1) val page: Int
|
@field:Min(1) val page: Int
|
||||||
)
|
)
|
||||||
@ -113,8 +117,8 @@ class SearchController(
|
|||||||
|
|
||||||
data class SearchQuery(
|
data class SearchQuery(
|
||||||
@field:NotBlank @field:Size(max = 300) val logicalOperator: String,
|
@field:NotBlank @field:Size(max = 300) val logicalOperator: String,
|
||||||
@Valid val predicates: List<SearchPredicate>,
|
@Valid @field:Size(max = 10) val predicates: List<SearchPredicate>,
|
||||||
@Valid @field:ValidChildQueriesDepth val childQueries: List<SearchQuery>?
|
@Valid @field:ValidChildQueriesDepth @field:Size(max = 10) val childQueries: List<SearchQuery>?
|
||||||
)
|
)
|
||||||
|
|
||||||
data class SearchPredicate(
|
data class SearchPredicate(
|
||||||
@ -138,11 +142,20 @@ class SearchController(
|
|||||||
if (apiVersion.isBlank())
|
if (apiVersion.isBlank())
|
||||||
return ResponseEntity.badRequest().build()
|
return ResponseEntity.badRequest().build()
|
||||||
|
|
||||||
|
// TODO: CSRF
|
||||||
|
|
||||||
|
val stopwatch = StopWatch()
|
||||||
|
stopwatch.start()
|
||||||
|
try {
|
||||||
val response = this.searchService.search(request)
|
val response = this.searchService.search(request)
|
||||||
|
|
||||||
return ResponseEntity.ok(response)
|
return ResponseEntity.ok(response)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
this.logger.error("Error while searching: {}", e.stackTraceToString())
|
||||||
|
return ResponseEntity.status(500).build()
|
||||||
|
} finally {
|
||||||
|
stopwatch.stop()
|
||||||
|
this.logger.info("Search took {} seconds", String.format("%.2f", stopwatch.totalTimeSeconds))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user