Skip to content

Commit f8ce0b9

Browse files
committed
Add panic message to description and avoid Headers duplication
1 parent 82137c2 commit f8ce0b9

File tree

4 files changed

+75
-5
lines changed

4 files changed

+75
-5
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ _vendor/bin
33
_vendor/pkg
44
_vendor/src/example
55
_vendor/src/github.com/fulldump/*
6+
7+
.idea

api.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package golax
22

33
import (
4+
"fmt"
45
"log"
56
"net/http"
67
"os"
@@ -46,7 +47,7 @@ func (a *Api) ServeHTTP(w http.ResponseWriter, r *http.Request) {
4647

4748
defer func(c *Context) {
4849
if r := recover(); r != nil {
49-
c.Error(http.StatusInternalServerError, string(debug.Stack()))
50+
c.Error(http.StatusInternalServerError, fmt.Sprintln(r)+string(debug.Stack()))
5051
a.Handler500(c)
5152
}
5253
}(c)

extended_writer.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ import "net/http"
77
* It wraps http.ResponseWriter to add: StatusCode & Length
88
*/
99
type ExtendedWriter struct {
10-
StatusCode int
11-
Length int
10+
StatusCode int
11+
statusCodeSent bool
12+
Length int
1213
http.ResponseWriter
1314
}
1415

1516
func NewExtendedWriter(w http.ResponseWriter) *ExtendedWriter {
16-
return &ExtendedWriter{200, 0, w}
17+
return &ExtendedWriter{200, false, 0, w}
1718
}
1819

1920
func (this *ExtendedWriter) Write(p []byte) (int, error) {
@@ -24,5 +25,8 @@ func (this *ExtendedWriter) Write(p []byte) (int, error) {
2425

2526
func (this *ExtendedWriter) WriteHeader(statusCode int) {
2627
this.StatusCode = statusCode
27-
this.ResponseWriter.WriteHeader(statusCode)
28+
if !this.statusCodeSent {
29+
this.ResponseWriter.WriteHeader(statusCode)
30+
this.statusCodeSent = true
31+
}
2832
}

extended_writer_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package golax
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"reflect"
7+
"strings"
8+
"testing"
9+
)
10+
11+
func Test_ExtendedWriter_WriteTwice(t *testing.T) {
12+
world := NewWorld()
13+
defer world.Destroy()
14+
15+
world.Api.Handler500 = func(c *Context) {
16+
json.NewEncoder(c.Response).Encode(map[string]interface{}{
17+
"status": c.LastError.StatusCode,
18+
"description": c.LastError.Description,
19+
})
20+
}
21+
22+
world.Api.Root.
23+
Interceptor(InterceptorError).
24+
Node("example").
25+
Method("GET", func(c *Context) {
26+
c.Response.WriteHeader(400)
27+
c.Response.WriteHeader(401)
28+
29+
c.Response.Write([]byte("Hello\n"))
30+
panic("This is a panic!")
31+
c.Response.Write([]byte("world"))
32+
33+
})
34+
35+
fmt.Println("=============================================")
36+
37+
res := world.Request("GET", "/example").Do()
38+
39+
if 400 != res.StatusCode {
40+
t.Error("Status code should be 400")
41+
}
42+
43+
body := res.BodyString()
44+
45+
body_lines := strings.Split(body, "\n")
46+
47+
if "Hello" != body_lines[0] {
48+
t.Error("First line should be `Hello` instead of " + body_lines[0])
49+
}
50+
51+
body_json := map[string]interface{}{}
52+
53+
json.Unmarshal([]byte(body_lines[1]), &body_json)
54+
55+
if !reflect.DeepEqual(float64(500), body_json["status"]) {
56+
t.Error("Body json status should be status:500")
57+
}
58+
59+
if !strings.HasPrefix(body_json["description"].(string), "This is a panic!") {
60+
t.Error("Body json description should start by `This is a panic!` ")
61+
}
62+
63+
}

0 commit comments

Comments
 (0)