Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
caddy
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Łukasz Nowak
caddy
Commits
29fec474
Commit
29fec474
authored
Mar 29, 2015
by
Matthew Holt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Detailed godoc; better error handling convention
parent
0a9a1930
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
27 additions
and
10 deletions
+27
-10
middleware/errors/errors.go
middleware/errors/errors.go
+1
-0
middleware/middleware.go
middleware/middleware.go
+18
-9
server/server.go
server/server.go
+8
-1
No files found.
middleware/errors/errors.go
View file @
29fec474
...
@@ -69,6 +69,7 @@ func (h ErrorHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, er
...
@@ -69,6 +69,7 @@ func (h ErrorHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, er
if
status
>=
400
{
if
status
>=
400
{
h
.
errorPage
(
w
,
status
)
h
.
errorPage
(
w
,
status
)
return
0
,
err
// status < 400 signals that a response has been written
}
}
return
status
,
err
return
status
,
err
...
...
middleware/middleware.go
View file @
29fec474
...
@@ -15,20 +15,29 @@ type (
...
@@ -15,20 +15,29 @@ type (
// HandlerFunc is like http.HandlerFunc except it returns a status code
// HandlerFunc is like http.HandlerFunc except it returns a status code
// and an error. It is the inner-most layer which serves individual
// and an error. It is the inner-most layer which serves individual
// requests. The status code is for the client's benefit
,
the error
// requests. The status code is for the client's benefit
;
the error
// value is for the server's benefit. The status code will be sent to
// value is for the server's benefit. The status code will be sent to
// the client while the error value will be logged privately. Sometimes,
// the client while the error value will be logged privately. Sometimes,
// an error status code (4xx or 5xx) may be returned with a nil error
// an error status code (4xx or 5xx) may be returned with a nil error
// when there is no reason to log the error on the server.
// when there is no reason to log the error on the server.
//
//
// If a HandlerFunc returns an error (status < 400), it should not write
// If a HandlerFunc returns an error (status >= 400), it should NOT
// to the response. This philosophy is what makes middleware.HandlerFunc
// write to the response. This philosophy makes middleware.HandlerFunc
// different from http.HandlerFunc. The error handling should happen
// different from http.HandlerFunc: error handling should happen at
// at the application layer or in a dedicated error-handling middleware
// the application layer or in dedicated error-handling middleware
// rather than an "every middleware for itself" paradigm. The error handling
// only, rather than with an "every middleware for itself" paradigm.
// logic should make sure that the client is properly responded to according
//
// to the status code, but should probably not reveal the error message. The
// The application or error-handling middleware should incorporate logic
// error message should be logged instead, for example.
// to ensure that the client always gets a proper response according to
// the status code. For security reasons, it should probably not reveal
// the actual error message. (Instead it should be logged, for example.)
//
// Handlers which do write to the response should return a status value
// < 400 as a signal that a response has been written. In other words,
// only error-handling middleware or the application will write to the
// response for a status code >= 400. When ANY handler writes to the
// response, it should return a status code < 400 to signal others to
// NOT write to the response again, which would be erroneous.
HandlerFunc
func
(
http
.
ResponseWriter
,
*
http
.
Request
)
(
int
,
error
)
HandlerFunc
func
(
http
.
ResponseWriter
,
*
http
.
Request
)
(
int
,
error
)
// Handler is like http.Handler except ServeHTTP returns a status code
// Handler is like http.Handler except ServeHTTP returns a status code
...
...
server/server.go
View file @
29fec474
...
@@ -5,6 +5,7 @@ package server
...
@@ -5,6 +5,7 @@ package server
import
(
import
(
"errors"
"errors"
"fmt"
"log"
"log"
"net/http"
"net/http"
"os"
"os"
...
@@ -116,7 +117,13 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
...
@@ -116,7 +117,13 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
http
.
StatusInternalServerError
)
http
.
StatusInternalServerError
)
}
}
}()
}()
s
.
stack
(
w
,
r
)
status
,
_
:=
s
.
stack
(
w
,
r
)
if
status
>=
400
{
w
.
WriteHeader
(
status
)
fmt
.
Fprintf
(
w
,
"%d %s"
,
status
,
http
.
StatusText
(
status
))
}
}
}
// buildStack builds the server's middleware stack based
// buildStack builds the server's middleware stack based
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment