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
857b4f90
Commit
857b4f90
authored
May 05, 2015
by
Matthew Holt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
errors: Log includes file and line number of panics
parent
97e702b9
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
39 additions
and
6 deletions
+39
-6
middleware/errors/errors.go
middleware/errors/errors.go
+39
-6
No files found.
middleware/errors/errors.go
View file @
857b4f90
...
@@ -7,6 +7,8 @@ import (
...
@@ -7,6 +7,8 @@ import (
"log"
"log"
"net/http"
"net/http"
"os"
"os"
"runtime"
"strings"
"github.com/mholt/caddy/middleware"
"github.com/mholt/caddy/middleware"
)
)
...
@@ -20,12 +22,7 @@ type ErrorHandler struct {
...
@@ -20,12 +22,7 @@ type ErrorHandler struct {
}
}
func
(
h
ErrorHandler
)
ServeHTTP
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
(
int
,
error
)
{
func
(
h
ErrorHandler
)
ServeHTTP
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
(
int
,
error
)
{
defer
func
()
{
defer
h
.
recovery
(
w
,
r
)
if
rec
:=
recover
();
rec
!=
nil
{
h
.
Log
.
Printf
(
"[PANIC %s] %v"
,
r
.
URL
.
String
(),
rec
)
h
.
errorPage
(
w
,
http
.
StatusInternalServerError
)
}
}()
status
,
err
:=
h
.
Next
.
ServeHTTP
(
w
,
r
)
status
,
err
:=
h
.
Next
.
ServeHTTP
(
w
,
r
)
...
@@ -78,4 +75,40 @@ func (h ErrorHandler) errorPage(w http.ResponseWriter, code int) {
...
@@ -78,4 +75,40 @@ func (h ErrorHandler) errorPage(w http.ResponseWriter, code int) {
http
.
Error
(
w
,
defaultBody
,
code
)
http
.
Error
(
w
,
defaultBody
,
code
)
}
}
func
(
h
ErrorHandler
)
recovery
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
rec
:=
recover
()
if
rec
==
nil
{
return
}
// Obtain source of panic
// From: https://gist.github.com/swdunlop/9629168
var
name
,
file
string
// function name, file name
var
line
int
var
pc
[
16
]
uintptr
n
:=
runtime
.
Callers
(
3
,
pc
[
:
])
for
_
,
pc
:=
range
pc
[
:
n
]
{
fn
:=
runtime
.
FuncForPC
(
pc
)
if
fn
==
nil
{
continue
}
file
,
line
=
fn
.
FileLine
(
pc
)
name
=
fn
.
Name
()
if
!
strings
.
HasPrefix
(
name
,
"runtime."
)
{
break
}
}
// Trim file path
delim
:=
"/caddy/"
pkgPathPos
:=
strings
.
Index
(
file
,
delim
)
if
pkgPathPos
>
-
1
&&
len
(
file
)
>
pkgPathPos
+
len
(
delim
)
{
file
=
file
[
pkgPathPos
+
len
(
delim
)
:
]
}
// Currently we don't use the function name, as file:line is more conventional
h
.
Log
.
Printf
(
"[PANIC %s] %s:%d - %v"
,
r
.
URL
.
String
(),
file
,
line
,
rec
)
h
.
errorPage
(
w
,
http
.
StatusInternalServerError
)
}
const
DefaultLogFilename
=
"error.log"
const
DefaultLogFilename
=
"error.log"
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