-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from davidchen0310/log_panic
Log the correct status code when panic
- Loading branch information
Showing
7 changed files
with
125 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package api | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
type logFunc func(statusCode int, err error) | ||
|
||
// logWithCheckingPanic attemps to recover from a possible panic, | ||
// modifies statusCode and err if there was indeed a panic, | ||
// passes the possibly updated status and err to the logFunc, | ||
// then panics again if there was indeed a panic to | ||
// make UnaryInterceptor in server/server.go return "internal server error" to the client. | ||
func logWithCheckingPanic(f logFunc, statusCode int, err error) { | ||
if r := recover(); r != nil { | ||
statusCode = http.StatusInternalServerError | ||
err = fmt.Errorf("panic: %v", r) | ||
defer panic(r) | ||
} | ||
f(statusCode, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package api | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
) | ||
|
||
func TestLogWithCheckingPanic(t *testing.T) { | ||
t.Parallel() | ||
testCases := []struct { | ||
name string | ||
input interface{} | ||
want string // See logStr below for the format | ||
}{ | ||
{ | ||
name: "panic with string", | ||
input: "string", | ||
want: "st: 500, err: panic: string", | ||
}, | ||
{ | ||
name: "panic with error", | ||
input: errors.New("error"), | ||
want: "st: 500, err: panic: error", | ||
}, | ||
{ | ||
name: "no panic", | ||
input: nil, | ||
want: "st: 200, err: <nil>", // See inputStatusCode below for 200 | ||
}, | ||
} | ||
const ( | ||
logStr = "st: %d, err: %v" | ||
inputStatusCode = http.StatusOK | ||
) | ||
var inputError error | ||
|
||
for _, tc := range testCases { | ||
// https://github.com/golang/go/wiki/CommonMistakes#using-goroutines-on-loop-iterator-variables | ||
tc := tc | ||
t.Run(tc.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
got := "" | ||
f := func(statusCode int, err error) { | ||
got = fmt.Sprintf(logStr, statusCode, err) | ||
} | ||
|
||
defer func() { | ||
// Capture the panic thrown from logWithCheckingPanic. | ||
recover() | ||
if got != tc.want { | ||
t.Errorf("got: %q, want: %q", got, tc.want) | ||
} | ||
}() | ||
defer logWithCheckingPanic(f, inputStatusCode, inputError) | ||
panic(tc.input) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters