mirror of
https://github.com/coredns/coredns.git
synced 2025-11-28 14:44:12 -05:00
Add middleware.NextOrFailure (#462)
This checks if the next middleware to be called is nil, and if so returns ServerFailure and an error. This makes the next calling more robust and saves some lines of code. Also prefix the error with the name of the middleware to aid in debugging.
This commit is contained in:
@@ -21,7 +21,7 @@ func setupWhoami(c *caddy.Controller) error {
|
||||
}
|
||||
|
||||
dnsserver.GetConfig(c).AddMiddleware(func(next middleware.Handler) middleware.Handler {
|
||||
return Whoami{Next: next}
|
||||
return Whoami{}
|
||||
})
|
||||
|
||||
return nil
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"net"
|
||||
"strconv"
|
||||
|
||||
"github.com/miekg/coredns/middleware"
|
||||
"github.com/miekg/coredns/request"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
@@ -15,9 +14,7 @@ import (
|
||||
|
||||
// Whoami is a middleware that returns your IP address, port and the protocol used for connecting
|
||||
// to CoreDNS.
|
||||
type Whoami struct {
|
||||
Next middleware.Handler
|
||||
}
|
||||
type Whoami struct{}
|
||||
|
||||
// ServeDNS implements the middleware.Handler interface.
|
||||
func (wh Whoami) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
|
||||
|
||||
@@ -3,7 +3,6 @@ package whoami
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/miekg/coredns/middleware"
|
||||
"github.com/miekg/coredns/middleware/pkg/dnsrecorder"
|
||||
"github.com/miekg/coredns/middleware/test"
|
||||
|
||||
@@ -15,7 +14,6 @@ func TestWhoami(t *testing.T) {
|
||||
wh := Whoami{}
|
||||
|
||||
tests := []struct {
|
||||
next middleware.Handler
|
||||
qname string
|
||||
qtype uint16
|
||||
expectedCode int
|
||||
@@ -23,7 +21,6 @@ func TestWhoami(t *testing.T) {
|
||||
expectedErr error
|
||||
}{
|
||||
{
|
||||
next: test.NextHandler(dns.RcodeSuccess, nil),
|
||||
qname: "example.org",
|
||||
qtype: dns.TypeA,
|
||||
expectedCode: dns.RcodeSuccess,
|
||||
@@ -35,7 +32,6 @@ func TestWhoami(t *testing.T) {
|
||||
ctx := context.TODO()
|
||||
|
||||
for i, tc := range tests {
|
||||
wh.Next = tc.next
|
||||
req := new(dns.Msg)
|
||||
req.SetQuestion(dns.Fqdn(tc.qname), tc.qtype)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user