2017-10-04 19:35:59 -04:00
|
|
|
package handlers
|
2015-06-25 22:36:58 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2017-10-09 21:10:59 -04:00
|
|
|
"fmt"
|
2015-06-25 22:36:58 -04:00
|
|
|
"log"
|
|
|
|
"net/http"
|
2017-10-05 21:08:17 -04:00
|
|
|
"strings"
|
2015-06-25 22:36:58 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type Error struct {
|
|
|
|
ErrorId int
|
|
|
|
ErrorString string
|
|
|
|
}
|
|
|
|
|
2017-10-09 21:10:59 -04:00
|
|
|
func (e *Error) Error() string {
|
|
|
|
return fmt.Sprintf("Error %d: %s", e.ErrorId, e.ErrorString)
|
|
|
|
}
|
|
|
|
|
2017-10-05 21:08:17 -04:00
|
|
|
func (e *Error) Read(json_str string) error {
|
|
|
|
dec := json.NewDecoder(strings.NewReader(json_str))
|
|
|
|
return dec.Decode(e)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Error) Write(w http.ResponseWriter) error {
|
|
|
|
enc := json.NewEncoder(w)
|
|
|
|
return enc.Encode(e)
|
|
|
|
}
|
|
|
|
|
2015-06-25 22:36:58 -04:00
|
|
|
var error_codes = map[int]string{
|
|
|
|
1: "Not Signed In",
|
|
|
|
2: "Unauthorized Access",
|
|
|
|
3: "Invalid Request",
|
|
|
|
4: "User Exists",
|
2017-02-19 07:50:36 -05:00
|
|
|
// 5: "Connection Failed", //reserved for client-side error
|
|
|
|
6: "Import Error",
|
2017-10-12 20:47:44 -04:00
|
|
|
7: "In Use Error",
|
2015-06-25 22:36:58 -04:00
|
|
|
999: "Internal Error",
|
|
|
|
}
|
|
|
|
|
|
|
|
func WriteError(w http.ResponseWriter, error_code int) {
|
|
|
|
msg, ok := error_codes[error_code]
|
|
|
|
if !ok {
|
|
|
|
log.Printf("Error: WriteError received error code of %d", error_code)
|
|
|
|
msg = error_codes[999]
|
|
|
|
}
|
|
|
|
e := Error{error_code, msg}
|
|
|
|
|
2017-10-05 21:08:17 -04:00
|
|
|
err := e.Write(w)
|
2015-06-25 22:36:58 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|