Improve response methods

This commit is contained in:
Frédéric Guillot 2017-12-22 20:17:48 -08:00
parent 589aee9f89
commit 3f473e4a09
3 changed files with 13 additions and 12 deletions

View File

@ -26,8 +26,8 @@ func (h *HTMLResponse) Render(template string, args map[string]interface{}) {
// ServerError sends a 500 error to the browser. // ServerError sends a 500 error to the browser.
func (h *HTMLResponse) ServerError(err error) { func (h *HTMLResponse) ServerError(err error) {
h.writer.WriteHeader(http.StatusInternalServerError)
h.writer.Header().Set("Content-Type", "text/html; charset=utf-8") h.writer.Header().Set("Content-Type", "text/html; charset=utf-8")
h.writer.WriteHeader(http.StatusInternalServerError)
if err != nil { if err != nil {
logger.Error("[Internal Server Error] %v", err) logger.Error("[Internal Server Error] %v", err)
@ -39,8 +39,8 @@ func (h *HTMLResponse) ServerError(err error) {
// BadRequest sends a 400 error to the browser. // BadRequest sends a 400 error to the browser.
func (h *HTMLResponse) BadRequest(err error) { func (h *HTMLResponse) BadRequest(err error) {
h.writer.WriteHeader(http.StatusBadRequest)
h.writer.Header().Set("Content-Type", "text/html; charset=utf-8") h.writer.Header().Set("Content-Type", "text/html; charset=utf-8")
h.writer.WriteHeader(http.StatusBadRequest)
if err != nil { if err != nil {
logger.Error("[Bad Request] %v", err) logger.Error("[Bad Request] %v", err)
@ -52,14 +52,14 @@ func (h *HTMLResponse) BadRequest(err error) {
// NotFound sends a 404 error to the browser. // NotFound sends a 404 error to the browser.
func (h *HTMLResponse) NotFound() { func (h *HTMLResponse) NotFound() {
h.writer.WriteHeader(http.StatusNotFound)
h.writer.Header().Set("Content-Type", "text/html; charset=utf-8") h.writer.Header().Set("Content-Type", "text/html; charset=utf-8")
h.writer.WriteHeader(http.StatusNotFound)
h.writer.Write([]byte("Page Not Found")) h.writer.Write([]byte("Page Not Found"))
} }
// Forbidden sends a 403 error to the browser. // Forbidden sends a 403 error to the browser.
func (h *HTMLResponse) Forbidden() { func (h *HTMLResponse) Forbidden() {
h.writer.WriteHeader(http.StatusForbidden)
h.writer.Header().Set("Content-Type", "text/html; charset=utf-8") h.writer.Header().Set("Content-Type", "text/html; charset=utf-8")
h.writer.WriteHeader(http.StatusForbidden)
h.writer.Write([]byte("Access Forbidden")) h.writer.Write([]byte("Access Forbidden"))
} }

View File

@ -20,29 +20,29 @@ type JSONResponse struct {
// Standard sends a JSON response with the status code 200. // Standard sends a JSON response with the status code 200.
func (j *JSONResponse) Standard(v interface{}) { func (j *JSONResponse) Standard(v interface{}) {
j.writer.WriteHeader(http.StatusOK)
j.commonHeaders() j.commonHeaders()
j.writer.WriteHeader(http.StatusOK)
j.writer.Write(j.toJSON(v)) j.writer.Write(j.toJSON(v))
} }
// Created sends a JSON response with the status code 201. // Created sends a JSON response with the status code 201.
func (j *JSONResponse) Created(v interface{}) { func (j *JSONResponse) Created(v interface{}) {
j.writer.WriteHeader(http.StatusCreated)
j.commonHeaders() j.commonHeaders()
j.writer.WriteHeader(http.StatusCreated)
j.writer.Write(j.toJSON(v)) j.writer.Write(j.toJSON(v))
} }
// NoContent sends a JSON response with the status code 204. // NoContent sends a JSON response with the status code 204.
func (j *JSONResponse) NoContent() { func (j *JSONResponse) NoContent() {
j.writer.WriteHeader(http.StatusNoContent)
j.commonHeaders() j.commonHeaders()
j.writer.WriteHeader(http.StatusNoContent)
} }
// BadRequest sends a JSON response with the status code 400. // BadRequest sends a JSON response with the status code 400.
func (j *JSONResponse) BadRequest(err error) { func (j *JSONResponse) BadRequest(err error) {
logger.Error("[Bad Request] %v", err) logger.Error("[Bad Request] %v", err)
j.writer.WriteHeader(http.StatusBadRequest)
j.commonHeaders() j.commonHeaders()
j.writer.WriteHeader(http.StatusBadRequest)
if err != nil { if err != nil {
j.writer.Write(j.encodeError(err)) j.writer.Write(j.encodeError(err))
@ -52,16 +52,16 @@ func (j *JSONResponse) BadRequest(err error) {
// NotFound sends a JSON response with the status code 404. // NotFound sends a JSON response with the status code 404.
func (j *JSONResponse) NotFound(err error) { func (j *JSONResponse) NotFound(err error) {
logger.Error("[Not Found] %v", err) logger.Error("[Not Found] %v", err)
j.writer.WriteHeader(http.StatusNotFound)
j.commonHeaders() j.commonHeaders()
j.writer.WriteHeader(http.StatusNotFound)
j.writer.Write(j.encodeError(err)) j.writer.Write(j.encodeError(err))
} }
// ServerError sends a JSON response with the status code 500. // ServerError sends a JSON response with the status code 500.
func (j *JSONResponse) ServerError(err error) { func (j *JSONResponse) ServerError(err error) {
logger.Error("[Internal Server Error] %v", err) logger.Error("[Internal Server Error] %v", err)
j.writer.WriteHeader(http.StatusInternalServerError)
j.commonHeaders() j.commonHeaders()
j.writer.WriteHeader(http.StatusInternalServerError)
if err != nil { if err != nil {
j.writer.Write(j.encodeError(err)) j.writer.Write(j.encodeError(err))
@ -71,14 +71,14 @@ func (j *JSONResponse) ServerError(err error) {
// Forbidden sends a JSON response with the status code 403. // Forbidden sends a JSON response with the status code 403.
func (j *JSONResponse) Forbidden() { func (j *JSONResponse) Forbidden() {
logger.Info("[API:Forbidden]") logger.Info("[API:Forbidden]")
j.writer.WriteHeader(http.StatusForbidden)
j.commonHeaders() j.commonHeaders()
j.writer.WriteHeader(http.StatusForbidden)
j.writer.Write(j.encodeError(errors.New("Access Forbidden"))) j.writer.Write(j.encodeError(errors.New("Access Forbidden")))
} }
func (j *JSONResponse) commonHeaders() { func (j *JSONResponse) commonHeaders() {
j.writer.Header().Set("Accept", "application/json") j.writer.Header().Set("Accept", "application/json")
j.writer.Header().Set("Content-Type", "application/json") j.writer.Header().Set("Content-Type", "application/json; charset=utf-8")
} }
func (j *JSONResponse) encodeError(err error) []byte { func (j *JSONResponse) encodeError(err error) []byte {

View File

@ -48,6 +48,7 @@ func (r *Response) Redirect(path string) {
// NotModified sends a response with a 304 status code. // NotModified sends a response with a 304 status code.
func (r *Response) NotModified() { func (r *Response) NotModified() {
r.commonHeaders()
r.writer.WriteHeader(http.StatusNotModified) r.writer.WriteHeader(http.StatusNotModified)
} }