fixed error code, validate neighbors query
This commit is contained in:
parent
538cefe098
commit
452dfbfb66
@ -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
|
||||
|
@ -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,
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user