fixed error code, validate neighbors query

This commit is contained in:
Annika Hannig 2022-06-20 18:00:41 +02:00
parent 538cefe098
commit 452dfbfb66
3 changed files with 30 additions and 0 deletions

View File

@ -53,6 +53,11 @@ func (s *Server) apiLookupPrefixGlobal(
}
} else {
// Query by neighbors
q, err = validateNeighborsQuery(q)
if err != nil {
return nil, err
}
neighbors, err := s.neighborsStore.LookupNeighbors(ctx, q)
if err != nil {
return nil, err

View File

@ -33,6 +33,7 @@ const (
TagConnectionRefused = "CONNECTION_REFUSED"
TagConnectionTimeout = "CONNECTION_TIMEOUT"
TagResourceNotFound = "NOT_FOUND"
TagValidationError = "VALIDATION_ERROR"
)
// Error codes
@ -40,6 +41,7 @@ const (
CodeGeneric = 42
CodeConnectionRefused = 100
CodeConnectionTimeout = 101
CodeValidationError = 400
CodeResourceNotFound = 404
)
@ -47,6 +49,7 @@ const (
const (
StatusError = http.StatusInternalServerError
StatusResourceNotFound = http.StatusNotFound
StatusValidationError = http.StatusBadRequest
)
// Handle an error and create a error API response
@ -76,6 +79,19 @@ func apiErrorResponse(
}
}
switch err {
case ErrQueryTooShort:
tag = TagValidationError
code = CodeValidationError
status = StatusValidationError
message = "the query is too short"
case ErrQueryIncomplete:
tag = TagValidationError
code = CodeValidationError
status = StatusValidationError
message = "the query is incomplete"
}
return api.ErrorResponse{
Code: code,
Tag: tag,

View File

@ -59,3 +59,12 @@ func validatePrefixQuery(value string) (string, error) {
}
return value, nil
}
// Helper: Validate neighbors query. A valid query should have
// at least 4 chars.
func validateNeighborsQuery(value string) (string, error) {
if len(value) < 4 { // Maybe make configurable
return "", ErrQueryTooShort
}
return value, nil
}