github.com/elliott5/community@v0.14.1-0.20160709191136-823126fb026a/wordsmith/api/response.go (about)

     1  // Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
     2  //
     3  // This software (Documize Community Edition) is licensed under 
     4  // GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
     5  //
     6  // You can operate outside the AGPL restrictions by purchasing
     7  // Documize Enterprise Edition and obtaining a commercial license
     8  // by contacting <sales@documize.com>. 
     9  //
    10  // https://documize.com
    11  
    12  package api
    13  
    14  import (
    15  	"github.com/documize/community/wordsmith/log"
    16  	"encoding/json"
    17  	"net/http"
    18  )
    19  
    20  // apiJSONResponse is the structure of a JSON response to a Documize client.
    21  type apiJSONResponse struct {
    22  	Code    int
    23  	Success bool
    24  	Message string
    25  	Data    interface{}
    26  }
    27  
    28  // SetJSONResponse sets the response type to "application/json" in the HTTP header.
    29  func SetJSONResponse(w http.ResponseWriter) {
    30  	w.Header().Set("Content-Type", "application/json; charset=utf-8")
    31  }
    32  
    33  // WriteError to the http.ResponseWriter, taking care to provide the correct
    34  // response error code within the JSON response.
    35  func WriteError(w http.ResponseWriter, err error) {
    36  
    37  	response := apiJSONResponse{}
    38  	response.Message = err.Error()
    39  	response.Success = false
    40  	response.Data = nil
    41  
    42  	switch err.Error() {
    43  	case "BadRequest":
    44  		response.Code = 400
    45  		w.WriteHeader(http.StatusBadRequest)
    46  	case "Unauthorized":
    47  		response.Code = 401
    48  		w.WriteHeader(http.StatusUnauthorized)
    49  	case "Forbidden":
    50  		response.Code = 403
    51  		w.WriteHeader(http.StatusForbidden)
    52  	case "NotFound":
    53  		response.Code = 404
    54  		w.WriteHeader(http.StatusNotFound)
    55  	default:
    56  		response.Code = 500
    57  		w.WriteHeader(http.StatusInternalServerError)
    58  	}
    59  
    60  	json, err := json.Marshal(response)
    61  	if err != nil {
    62  		log.Error("json.Marshal", err)
    63  	}
    64  
    65  	if _, err := w.Write(json); err != nil {
    66  		log.Error("write to ResponseWriter", err)
    67  	}
    68  }
    69  
    70  // WriteErrorBadRequest provides feedback to a Documize client on an error,
    71  // where that error is described in a string.
    72  func WriteErrorBadRequest(w http.ResponseWriter, message string) {
    73  
    74  	response := apiJSONResponse{}
    75  	response.Message = message
    76  	response.Success = false
    77  	response.Data = nil
    78  
    79  	response.Code = 400
    80  	w.WriteHeader(http.StatusBadRequest)
    81  
    82  	json, err := json.Marshal(response)
    83  	if err != nil {
    84  		log.Error("json.Marshal", err)
    85  	}
    86  
    87  	if _, err := w.Write(json); err != nil {
    88  		log.Error("write to ResponseWriter", err)
    89  	}
    90  }