Code review comment for lp:~hduran-8/goose/json_encoded_errors

Revision history for this message
Horacio DurĂ¡n (hduran-8) wrote :

Reviewers: mp+217860_code.launchpad.net,

Message:
Please take a look.

Description:
Added json encoded for ServerErrors

added jsonEncode function to novaservice that
tries to convert the given error into a
ServerError and call its AsJSON method or
wrap the given error into a 500 ServerError
which will contain the original error message.

https://code.launchpad.net/~hduran-8/goose/json_encoded_errors/+merge/217860

Requires:
https://code.launchpad.net/~hduran-8/goose/testservice_errors/+merge/217818

(do not edit description out of merge proposal)

Please review this at https://codereview.appspot.com/97870044/

Affected files (+18, -0 lines):
   A [revision details]
   M testservices/errors.go
   M testservices/novaservice/service.go

Index: [revision details]
=== added file '[revision details]'
--- [revision details] 2012-01-01 00:00:00 +0000
+++ [revision details] 2012-01-01 00:00:00 +0000
@@ -0,0 +1,2 @@
+Old revision: <email address hidden>
+New revision: <email address hidden>

Index: testservices/errors.go
=== modified file 'testservices/errors.go'
--- testservices/errors.go 2014-04-30 23:15:55 +0000
+++ testservices/errors.go 2014-05-01 00:43:30 +0000
@@ -27,6 +27,10 @@
   return &ServerError{code: code, message: fmt.Sprintf(message, args...)}
  }

+func (n *ServerError) AsJSON() string {
+ return fmt.Sprintf(`{%q:{"message":%q, "code":%d}}`, n.Name(), n.message,
n.code)
+}
+
  func (n *ServerError) Error() string {
   return fmt.Sprintf("%s: %s", n.Name(), n.message)
  }
@@ -39,6 +43,10 @@
   return name
  }

+func NewInternalServerError(message string) *ServerError {
+ return serverErrorf(500, message)
+}
+
  func NewNotFoundError(message string) *ServerError {
   return serverErrorf(404, message)
  }

Index: testservices/novaservice/service.go
=== modified file 'testservices/novaservice/service.go'
--- testservices/novaservice/service.go 2014-04-30 19:34:31 +0000
+++ testservices/novaservice/service.go 2014-05-01 00:43:30 +0000
@@ -33,6 +33,14 @@
   nextIPId int
  }

+func errorJSONEncode(err error) string {
+ serverError, ok := err.(*testservices.ServerError)
+ if !ok {
+ serverError = testservices.NewInternalServerError(err.Error())
+ }
+ return serverError.AsJSON()
+}
+
  // endpoint returns either a versioned or non-versioned service
  // endpoint URL from the given path.
  func (n *Nova) endpointURL(version bool, path string) string {

« Back to merge proposal