Skip to content

Commit

Permalink
DNS: add IsErrQueryNotFound function for easier error evaluation
Browse files Browse the repository at this point in the history
  • Loading branch information
yurkeen committed Jul 1, 2020
1 parent 8d18422 commit 10361dd
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 22 deletions.
12 changes: 3 additions & 9 deletions agent/consul/prepared_query_endpoint.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package consul

import (
"errors"
"fmt"
"strings"
"time"
Expand All @@ -16,11 +15,6 @@ import (
"github.com/hashicorp/go-uuid"
)

var (
// ErrQueryNotFound is returned if the query lookup failed.
ErrQueryNotFound = errors.New("Query not found")
)

// PreparedQuery manages the prepared query endpoint.
type PreparedQuery struct {
srv *Server
Expand Down Expand Up @@ -228,7 +222,7 @@ func (p *PreparedQuery) Get(args *structs.PreparedQuerySpecificRequest,
return err
}
if query == nil {
return ErrQueryNotFound
return structs.ErrQueryNotFound
}

// If no prefix ACL applies to this query, then they are
Expand Down Expand Up @@ -303,7 +297,7 @@ func (p *PreparedQuery) Explain(args *structs.PreparedQueryExecuteRequest,
return err
}
if query == nil {
return ErrQueryNotFound
return structs.ErrQueryNotFound
}

// Place the query into a list so we can run the standard ACL filter on
Expand Down Expand Up @@ -350,7 +344,7 @@ func (p *PreparedQuery) Execute(args *structs.PreparedQueryExecuteRequest,
return err
}
if query == nil {
return ErrQueryNotFound
return structs.ErrQueryNotFound
}

// Execute the query for the local DC.
Expand Down
12 changes: 6 additions & 6 deletions agent/consul/prepared_query_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func TestPreparedQuery_Apply(t *testing.T) {
}
var resp structs.IndexedPreparedQueries
if err = msgpackrpc.CallWithCodec(codec, "PreparedQuery.Get", req, &resp); err != nil {
if err.Error() != ErrQueryNotFound.Error() {
if !structs.IsErrQueryNotFound(err) {
t.Fatalf("err: %v", err)
}
}
Expand Down Expand Up @@ -317,7 +317,7 @@ func TestPreparedQuery_Apply_ACLDeny(t *testing.T) {
}
var resp structs.IndexedPreparedQueries
if err = msgpackrpc.CallWithCodec(codec, "PreparedQuery.Get", req, &resp); err != nil {
if err.Error() != ErrQueryNotFound.Error() {
if !structs.IsErrQueryNotFound(err) {
t.Fatalf("err: %v", err)
}
}
Expand Down Expand Up @@ -412,7 +412,7 @@ func TestPreparedQuery_Apply_ACLDeny(t *testing.T) {
}
var resp structs.IndexedPreparedQueries
if err = msgpackrpc.CallWithCodec(codec, "PreparedQuery.Get", req, &resp); err != nil {
if err.Error() != ErrQueryNotFound.Error() {
if !structs.IsErrQueryNotFound(err) {
t.Fatalf("err: %v", err)
}
}
Expand Down Expand Up @@ -1082,7 +1082,7 @@ func TestPreparedQuery_Get(t *testing.T) {
}
var resp structs.IndexedPreparedQueries
if err := msgpackrpc.CallWithCodec(codec, "PreparedQuery.Get", req, &resp); err != nil {
if err.Error() != ErrQueryNotFound.Error() {
if !structs.IsErrQueryNotFound(err) {
t.Fatalf("err: %v", err)
}
}
Expand Down Expand Up @@ -1429,7 +1429,7 @@ func TestPreparedQuery_Explain(t *testing.T) {
}
var resp structs.IndexedPreparedQueries
if err := msgpackrpc.CallWithCodec(codec, "PreparedQuery.Explain", req, &resp); err != nil {
if err.Error() != ErrQueryNotFound.Error() {
if !structs.IsErrQueryNotFound(err) {
t.Fatalf("err: %v", err)
}
}
Expand Down Expand Up @@ -1617,7 +1617,7 @@ func TestPreparedQuery_Execute(t *testing.T) {

var reply structs.PreparedQueryExecuteResponse
err := msgpackrpc.CallWithCodec(codec1, "PreparedQuery.Execute", &req, &reply)
assert.EqualError(t, err, ErrQueryNotFound.Error())
assert.EqualError(t, err, structs.ErrQueryNotFound.Error())
assert.Len(t, reply.Nodes, 0)
})

Expand Down
4 changes: 1 addition & 3 deletions agent/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/coredns/coredns/plugin/pkg/dnsutil"
cachetype "github.com/hashicorp/consul/agent/cache-types"
"github.com/hashicorp/consul/agent/config"
"github.com/hashicorp/consul/agent/consul"
"github.com/hashicorp/consul/agent/structs"
"github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/ipaddr"
Expand Down Expand Up @@ -828,8 +827,7 @@ func (d *DNSServer) computeRCode(err error) int {
if err == nil {
return dns.RcodeSuccess
}
dErr := err.Error()
if structs.IsErrNoDCPath(err) || dErr == consul.ErrQueryNotFound.Error() {
if structs.IsErrNoDCPath(err) || structs.IsErrQueryNotFound(err) {
return dns.RcodeNameError
}
return dns.RcodeServerFailure
Expand Down
7 changes: 3 additions & 4 deletions agent/prepared_query_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"strings"

cachetype "github.com/hashicorp/consul/agent/cache-types"
"github.com/hashicorp/consul/agent/consul"
"github.com/hashicorp/consul/agent/structs"
)

Expand Down Expand Up @@ -144,7 +143,7 @@ func (s *HTTPServer) preparedQueryExecute(id string, resp http.ResponseWriter, r
if err := s.agent.RPC("PreparedQuery.Execute", &args, &reply); err != nil {
// We have to check the string since the RPC sheds
// the specific error type.
if err.Error() == consul.ErrQueryNotFound.Error() {
if structs.IsErrQueryNotFound(err) {
resp.WriteHeader(http.StatusNotFound)
fmt.Fprint(resp, err.Error())
return nil, nil
Expand Down Expand Up @@ -198,7 +197,7 @@ RETRY_ONCE:
if err := s.agent.RPC("PreparedQuery.Explain", &args, &reply); err != nil {
// We have to check the string since the RPC sheds
// the specific error type.
if err.Error() == consul.ErrQueryNotFound.Error() {
if structs.IsErrQueryNotFound(err) {
resp.WriteHeader(http.StatusNotFound)
fmt.Fprint(resp, err.Error())
return nil, nil
Expand Down Expand Up @@ -229,7 +228,7 @@ RETRY_ONCE:
if err := s.agent.RPC("PreparedQuery.Get", &args, &reply); err != nil {
// We have to check the string since the RPC sheds
// the specific error type.
if err.Error() == consul.ErrQueryNotFound.Error() {
if structs.IsErrQueryNotFound(err) {
resp.WriteHeader(http.StatusNotFound)
fmt.Fprint(resp, err.Error())
return nil, nil
Expand Down
6 changes: 6 additions & 0 deletions agent/structs/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const (
errSegmentsNotSupported = "Network segments are not supported in this version of Consul"
errRPCRateExceeded = "RPC rate limit exceeded"
errServiceNotFound = "Service not found: "
errQueryNotFound = "Query not found"
)

var (
Expand All @@ -24,12 +25,17 @@ var (
ErrSegmentsNotSupported = errors.New(errSegmentsNotSupported)
ErrRPCRateExceeded = errors.New(errRPCRateExceeded)
ErrDCNotAvailable = errors.New(errDCNotAvailable)
ErrQueryNotFound = errors.New(errQueryNotFound)
)

func IsErrNoDCPath(err error) bool {
return err != nil && strings.Contains(err.Error(), errNoDCPath)
}

func IsErrQueryNotFound(err error) bool {
return err != nil && strings.Contains(err.Error(), errQueryNotFound)
}

func IsErrNoLeader(err error) bool {
return err != nil && strings.Contains(err.Error(), errNoLeader)
}
Expand Down

0 comments on commit 10361dd

Please sign in to comment.