github.com/jancarloviray/community@v0.41.1-0.20170124221257-33a66c87cf2f/core/api/endpoint/init.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 endpoint 13 14 import ( 15 "fmt" 16 "net/http" 17 18 "github.com/documize/community/core/api/store" 19 "github.com/documize/community/core/log" 20 ) 21 22 var storageProvider store.StorageProvider 23 24 func init() { 25 storageProvider = new(store.LocalStorageProvider) 26 } 27 28 func writePayloadError(w http.ResponseWriter, method string, err error) { 29 w.Header().Set("Content-Type", "application/json; charset=utf-8") 30 w.WriteHeader(http.StatusBadRequest) 31 _, err2 := w.Write([]byte("{Error: 'Bad payload'}")) 32 log.IfErr(err2) 33 log.Error(fmt.Sprintf("Unable to decode HTML request body for method %s", method), err) 34 } 35 36 func writeTransactionError(w http.ResponseWriter, method string, err error) { 37 w.Header().Set("Content-Type", "application/json; charset=utf-8") 38 w.WriteHeader(http.StatusInternalServerError) 39 _, err2 := w.Write([]byte("{Error: 'No transaction'}")) 40 log.IfErr(err2) 41 log.Error(fmt.Sprintf("Unable to get database transaction for method %s", method), err) 42 } 43 44 /* 45 func WriteAddRecordError(w http.ResponseWriter, method string, err error) { 46 w.Header().Set("Content-Type", "application/json; charset=utf-8") 47 w.WriteHeader(http.StatusInternalServerError) 48 _, err2 := w.Write([]byte("{Error: 'Add error'}")) 49 log.IfErr(err2) 50 log.Error(fmt.Sprintf("Unable to insert new database record for method %s", method), err) 51 } 52 53 func WriteGetRecordError(w http.ResponseWriter, method, entity string, err error) { 54 w.Header().Set("Content-Type", "application/json; charset=utf-8") 55 w.WriteHeader(http.StatusInternalServerError) 56 _, err2 := w.Write([]byte("{Error: 'Get error'}")) 57 log.IfErr(err2) 58 log.Error(fmt.Sprintf("Unable to get %s record for method %s", entity, method), err) 59 } 60 61 func WriteUpdateRecordError(w http.ResponseWriter, method string, id string, err error) { 62 w.Header().Set("Content-Type", "application/json; charset=utf-8") 63 w.WriteHeader(http.StatusInternalServerError) 64 _, err2 := w.Write([]byte("{Error: 'Add error'}")) 65 log.IfErr(err2) 66 log.Error(fmt.Sprintf("Unable to update database record ID %s for method %s", id, method), err) 67 } 68 69 func WriteParameterParsingError(w http.ResponseWriter, method, parameter string, err error) { 70 w.Header().Set("Content-Type", "application/json; charset=utf-8") 71 w.WriteHeader(http.StatusBadRequest) 72 _, err2 := w.Write([]byte("{Error: 'Bad parameter'}")) 73 log.IfErr(err2) 74 log.Error(fmt.Sprintf("Unable to parse API parameter %s for method %s", parameter, method), err) 75 } 76 */ 77 78 func writeMissingDataError(w http.ResponseWriter, method, parameter string) { 79 w.Header().Set("Content-Type", "application/json; charset=utf-8") 80 w.WriteHeader(http.StatusBadRequest) 81 _, err := w.Write([]byte("{Error: 'Missing data'}")) 82 log.IfErr(err) 83 log.Info(fmt.Sprintf("Missing data %s for method %s", parameter, method)) 84 } 85 86 func writeNotFoundError(w http.ResponseWriter, method string, id string) { 87 w.Header().Set("Content-Type", "application/json; charset=utf-8") 88 w.WriteHeader(http.StatusNotFound) 89 _, err := w.Write([]byte("{Error: 'Not found'}")) 90 log.IfErr(err) 91 log.Info(fmt.Sprintf("Not found ID %s for method %s", id, method)) 92 } 93 94 func writeGeneralSQLError(w http.ResponseWriter, method string, err error) { 95 w.Header().Set("Content-Type", "application/json; charset=utf-8") 96 w.WriteHeader(http.StatusBadRequest) 97 _, err2 := w.Write([]byte("{Error: 'SQL error'}")) 98 log.IfErr(err2) 99 log.Error(fmt.Sprintf("General SQL error for method %s", method), err) 100 } 101 102 func writeJSONMarshalError(w http.ResponseWriter, method, entity string, err error) { 103 w.Header().Set("Content-Type", "application/json; charset=utf-8") 104 w.WriteHeader(http.StatusBadRequest) 105 _, err2 := w.Write([]byte("{Error: 'JSON marshal failed'}")) 106 log.IfErr(err2) 107 log.Error(fmt.Sprintf("Failed to JSON marshal %s for method %s", entity, method), err) 108 } 109 110 func writeServerError(w http.ResponseWriter, method string, err error) { 111 w.Header().Set("Content-Type", "application/json; charset=utf-8") 112 w.WriteHeader(http.StatusBadRequest) 113 _, err2 := w.Write([]byte("{Error: 'Internal server error'}")) 114 log.IfErr(err2) 115 log.Error(fmt.Sprintf("Internal server error for method %s", method), err) 116 } 117 118 func writeDuplicateError(w http.ResponseWriter, method, entity string) { 119 w.Header().Set("Content-Type", "application/json; charset=utf-8") 120 w.WriteHeader(http.StatusConflict) 121 _, err := w.Write([]byte("{Error: 'Duplicate record'}")) 122 log.IfErr(err) 123 log.Info(fmt.Sprintf("Duplicate %s record detected for method %s", entity, method)) 124 } 125 126 func writeUnauthorizedError(w http.ResponseWriter) { 127 w.Header().Set("Content-Type", "application/json; charset=utf-8") 128 w.WriteHeader(http.StatusUnauthorized) 129 _, err := w.Write([]byte("{Error: 'Unauthorized'}")) 130 log.IfErr(err) 131 } 132 133 func writeForbiddenError(w http.ResponseWriter) { 134 w.Header().Set("Content-Type", "application/json; charset=utf-8") 135 w.WriteHeader(http.StatusForbidden) 136 _, err := w.Write([]byte("{Error: 'Forbidden'}")) 137 log.IfErr(err) 138 } 139 140 func writeBadRequestError(w http.ResponseWriter, method, message string) { 141 w.Header().Set("Content-Type", "application/json; charset=utf-8") 142 w.WriteHeader(http.StatusBadRequest) 143 _, err := w.Write([]byte("{Error: 'Bad Request'}")) 144 log.IfErr(err) 145 log.Info(fmt.Sprintf("Bad Request %s for method %s", message, method)) 146 } 147 148 func writeSuccessBytes(w http.ResponseWriter, data []byte) { 149 w.Header().Set("Content-Type", "application/json; charset=utf-8") 150 w.WriteHeader(http.StatusOK) 151 _, err := w.Write(data) 152 log.IfErr(err) 153 } 154 155 func writeSuccessString(w http.ResponseWriter, data string) { 156 w.Header().Set("Content-Type", "application/json; charset=utf-8") 157 w.WriteHeader(http.StatusOK) 158 _, err := w.Write([]byte(data)) 159 log.IfErr(err) 160 } 161 162 func writeSuccessEmptyJSON(w http.ResponseWriter) { 163 w.Header().Set("Content-Type", "application/json; charset=utf-8") 164 w.WriteHeader(http.StatusOK) 165 _, err := w.Write([]byte("{}")) 166 log.IfErr(err) 167 }