From 2334d9f7a19435f3f30a7d3684e0c3273c4dfb5e Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Sat, 16 Jul 2016 14:44:48 +0100 Subject: [PATCH] Update errors and error messages --- db/db.go | 16 ++++------------ kvs/err.go | 8 ++++---- web/routes.go | 28 ++++++++++++++++------------ 3 files changed, 24 insertions(+), 28 deletions(-) diff --git a/db/db.go b/db/db.go index f9d29b45..cbce0e43 100644 --- a/db/db.go +++ b/db/db.go @@ -120,16 +120,16 @@ func status(e error) interface{} { switch e.(type) { default: return "OK" - case error: - return "ERR" case *kvs.DBError: return "ERR_DB" case *kvs.TXError: - return "ERR_DB" + return "ERR_TX" case *kvs.CKError: return "ERR_CK" case *kvs.KVError: - return "ERR_EXISTS" + return "ERR_KV" + case error: + return "ERR" } } @@ -139,14 +139,6 @@ func detail(e error) interface{} { return nil case error: return err.Error() - case *kvs.DBError: - return "A database error occured" - case *kvs.TXError: - return "A database error occured" - case *kvs.CKError: - return "A cipherkey error occured" - case *kvs.KVError: - return "The item already exists" } } diff --git a/kvs/err.go b/kvs/err.go index 832ab941..7643fb8a 100644 --- a/kvs/err.go +++ b/kvs/err.go @@ -35,7 +35,7 @@ type DBError struct { // Error returns the string representation of the error. func (e *DBError) Error() string { - return fmt.Sprintf("There was a problem performing a read or write with the database: %s", e.Err.Error()) + return fmt.Sprintf("There was a problem writing to the database") } // TXError is an error which occurs when there is a @@ -46,7 +46,7 @@ type TXError struct { // Error returns the string representation of the error. func (e *TXError) Error() string { - return fmt.Sprintf("There was a problem performing a read or write with the transaction: %s", e.Err.Error()) + return fmt.Sprintf("There was a problem with the transaction") } // CKError is an error which occurs when there is a @@ -57,7 +57,7 @@ type CKError struct { // Error returns the string representation of the error. func (e *CKError) Error() string { - return fmt.Sprintf("There was a problem performing a read or write with the cipherkey: %s", e.Err.Error()) + return fmt.Sprintf("This cipherkey used is not valid") } // KVError is an error which occurs when there is a @@ -71,5 +71,5 @@ type KVError struct { // Error returns the string representation of the error. func (e *KVError) Error() string { - return fmt.Sprintf("Key `%s` already exists. Expecting `%s`, but found `%s`", e.Key, e.Exp, e.Val) + return fmt.Sprintf("Database record already exists.") } diff --git a/web/routes.go b/web/routes.go index a24ba78e..76cb84fa 100644 --- a/web/routes.go +++ b/web/routes.go @@ -24,9 +24,13 @@ func output(c *fibre.Context, res interface{}) error { case "OK": return c.Send(200, ret.Result) case "ERR_DB": + return fibre.NewHTTPError(503) + case "ERR_TX": return fibre.NewHTTPError(500) - case "ERR_EXISTS": + case "ERR_KV": return fibre.NewHTTPError(409) + case "ERR_CK": + return fibre.NewHTTPError(403) default: return fibre.NewHTTPError(400) } @@ -73,8 +77,8 @@ func routes(s *fibre.Fibre) { s.Post("/sql", func(c *fibre.Context) error { res, err := db.Execute(c, c.Request().Body) if err != nil { - return c.Send(400, map[string]interface{}{ - "code": 400, + return c.Send(500, map[string]interface{}{ + "code": 500, "details": "Request problems detected", "documentation": "https://docs.surreal.io/", "information": err.Error(), @@ -95,7 +99,7 @@ func routes(s *fibre.Fibre) { sql := db.Prepare("SELECT * FROM ⟨%v⟩", c.Param("class")) res, err := db.Execute(c, sql) if err != nil { - return fibre.NewHTTPError(400) + return fibre.NewHTTPError(500) } return output(c, res[0]) }) @@ -104,7 +108,7 @@ func routes(s *fibre.Fibre) { sql := db.Prepare("CREATE ⟨%v⟩ CONTENT %v RETURN AFTER", c.Param("class"), string(c.Body())) res, err := db.Execute(c, sql) if err != nil { - return fibre.NewHTTPError(400) + return fibre.NewHTTPError(500) } return output(c, res[0]) }) @@ -113,7 +117,7 @@ func routes(s *fibre.Fibre) { sql := db.Prepare("DELETE FROM ⟨%v⟩", c.Param("class")) res, err := db.Execute(c, sql) if err != nil { - return fibre.NewHTTPError(400) + return fibre.NewHTTPError(500) } return output(c, res[0]) }) @@ -130,7 +134,7 @@ func routes(s *fibre.Fibre) { sql := db.Prepare("SELECT * FROM @⟨%v⟩:⟨%v⟩", c.Param("class"), c.Param("id")) res, err := db.Execute(c, sql) if err != nil { - return fibre.NewHTTPError(400) + return fibre.NewHTTPError(500) } return output(c, res[0]) }) @@ -139,7 +143,7 @@ func routes(s *fibre.Fibre) { sql := db.Prepare("CREATE @⟨%v⟩:⟨%v⟩ CONTENT %v RETURN AFTER", c.Param("class"), c.Param("id"), string(c.Body())) res, err := db.Execute(c, sql) if err != nil { - return fibre.NewHTTPError(400) + return fibre.NewHTTPError(500) } return output(c, res[0]) }) @@ -148,7 +152,7 @@ func routes(s *fibre.Fibre) { sql := db.Prepare("UPDATE @⟨%v⟩:⟨%v⟩ CONTENT %v RETURN AFTER", c.Param("class"), c.Param("id"), string(c.Body())) res, err := db.Execute(c, sql) if err != nil { - return fibre.NewHTTPError(400) + return fibre.NewHTTPError(500) } return output(c, res[0]) }) @@ -157,7 +161,7 @@ func routes(s *fibre.Fibre) { sql := db.Prepare("MODIFY @⟨%v⟩:⟨%v⟩ DIFF %v RETURN AFTER", c.Param("class"), c.Param("id"), string(c.Body())) res, err := db.Execute(c, sql) if err != nil { - return fibre.NewHTTPError(400) + return fibre.NewHTTPError(500) } return output(c, res[0]) }) @@ -166,7 +170,7 @@ func routes(s *fibre.Fibre) { sql := db.Prepare("SELECT HISTORY FROM @⟨%v⟩:⟨%v⟩", c.Param("class"), c.Param("id")) res, err := db.Execute(c, sql) if err != nil { - return fibre.NewHTTPError(400) + return fibre.NewHTTPError(500) } return output(c, res[0]) }) @@ -175,7 +179,7 @@ func routes(s *fibre.Fibre) { sql := db.Prepare("DELETE @⟨%v⟩:⟨%v⟩", c.Param("class"), c.Param("id")) res, err := db.Execute(c, sql) if err != nil { - return fibre.NewHTTPError(400) + return fibre.NewHTTPError(500) } return output(c, res[0]) })