Skip to content

Commit d2ee0dd

Browse files
committed
added status func
1 parent 8e360ed commit d2ee0dd

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

status.go

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

3-
import "net/http"
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
)
7+
8+
type errorResponse struct {
9+
StatusCode int `json:"status_code"`
10+
Status string `json:"status"`
11+
}
412

513
func ErrorStatus(w http.ResponseWriter) {
614
w.WriteHeader(http.StatusBadRequest)
715
}
16+
17+
func ErrorResponse(w http.ResponseWriter, statusCode int, err error) {
18+
response := errorResponse{
19+
StatusCode: statusCode,
20+
Status: err.Error(),
21+
}
22+
responseText, err := json.Marshal(response)
23+
if err != nil {
24+
ErrorResponse(w, 1, err)
25+
}
26+
27+
w.Write(responseText)
28+
}

0 commit comments

Comments
 (0)