mirror of
https://github.com/caddyserver/caddy.git
synced 2026-05-24 16:41:20 -04:00
44
caddyhttp/httpserver/error.go
Normal file
44
caddyhttp/httpserver/error.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package httpserver
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
var (
|
||||
_ error = NonHijackerError{}
|
||||
_ error = NonFlusherError{}
|
||||
_ error = NonCloseNotifierError{}
|
||||
)
|
||||
|
||||
// NonHijackerError is more descriptive error caused by a non hijacker
|
||||
type NonHijackerError struct {
|
||||
// underlying type which doesn't implement Hijack
|
||||
Underlying interface{}
|
||||
}
|
||||
|
||||
// Implement Error
|
||||
func (h NonHijackerError) Error() string {
|
||||
return fmt.Sprintf("%T is not a hijacker", h.Underlying)
|
||||
}
|
||||
|
||||
// NonFlusherError is more descriptive error caused by a non flusher
|
||||
type NonFlusherError struct {
|
||||
// underlying type which doesn't implement Flush
|
||||
Underlying interface{}
|
||||
}
|
||||
|
||||
// Implement Error
|
||||
func (f NonFlusherError) Error() string {
|
||||
return fmt.Sprintf("%T is not a flusher", f.Underlying)
|
||||
}
|
||||
|
||||
// NonCloseNotifierError is more descriptive error caused by a non closeNotifier
|
||||
type NonCloseNotifierError struct {
|
||||
// underlying type which doesn't implement CloseNotify
|
||||
Underlying interface{}
|
||||
}
|
||||
|
||||
// Implement Error
|
||||
func (c NonCloseNotifierError) Error() string {
|
||||
return fmt.Sprintf("%T is not a closeNotifier", c.Underlying)
|
||||
}
|
||||
@@ -2,7 +2,6 @@ package httpserver
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"errors"
|
||||
"net"
|
||||
"net/http"
|
||||
"time"
|
||||
@@ -75,7 +74,7 @@ func (r *ResponseRecorder) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
||||
if hj, ok := r.ResponseWriter.(http.Hijacker); ok {
|
||||
return hj.Hijack()
|
||||
}
|
||||
return nil, nil, errors.New("not a Hijacker")
|
||||
return nil, nil, NonHijackerError{Underlying: r.ResponseWriter}
|
||||
}
|
||||
|
||||
// Flush implements http.Flusher. It simply wraps the underlying
|
||||
@@ -84,7 +83,7 @@ func (r *ResponseRecorder) Flush() {
|
||||
if f, ok := r.ResponseWriter.(http.Flusher); ok {
|
||||
f.Flush()
|
||||
} else {
|
||||
panic("not a Flusher") // should be recovered at the beginning of middleware stack
|
||||
panic(NonFlusherError{Underlying: r.ResponseWriter}) // should be recovered at the beginning of middleware stack
|
||||
}
|
||||
}
|
||||
|
||||
@@ -94,5 +93,5 @@ func (r *ResponseRecorder) CloseNotify() <-chan bool {
|
||||
if cn, ok := r.ResponseWriter.(http.CloseNotifier); ok {
|
||||
return cn.CloseNotify()
|
||||
}
|
||||
panic("not a CloseNotifier")
|
||||
panic(NonCloseNotifierError{Underlying: r.ResponseWriter})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user