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
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: /code.launchpad .net/~hduran- 8/goose/ testservice_ errors/ +merge/ 217818
https:/
(do not edit description out of merge proposal)
Please review this at https:/ /codereview. appspot. com/97870044/
Affected files (+18, -0 lines): errors. go novaservice/ service. go
A [revision details]
M testservices/
M testservices/
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 errors. go' errors. go 2014-04-30 23:15:55 +0000 errors. go 2014-05-01 00:43:30 +0000 message, args...)}
=== modified file 'testservices/
--- testservices/
+++ testservices/
@@ -27,6 +27,10 @@
return &ServerError{code: code, message: fmt.Sprintf(
}
+func (n *ServerError) AsJSON() string { `{%q:{" message" :%q, "code":%d}}`, n.Name(), n.message,
+ return fmt.Sprintf(
n.code)
+}
+
func (n *ServerError) Error() string {
return fmt.Sprintf("%s: %s", n.Name(), n.message)
}
@@ -39,6 +43,10 @@
return name
}
+func NewInternalServ erError( message string) *ServerError { r(message string) *ServerError {
+ return serverErrorf(500, message)
+}
+
func NewNotFoundErro
return serverErrorf(404, message)
}
Index: testservices/ novaservice/ service. go novaservice/ service. go' novaservice/ service. go 2014-04-30 19:34:31 +0000 novaservice/ service. go 2014-05-01 00:43:30 +0000
=== modified file 'testservices/
--- testservices/
+++ testservices/
@@ -33,6 +33,14 @@
nextIPId int
}
+func errorJSONEncode(err error) string { ces.ServerError ) NewInternalServ erError( err.Error( )) AsJSON( )
+ serverError, ok := err.(*testservi
+ if !ok {
+ serverError = testservices.
+ }
+ return serverError.
+}
+
// endpoint returns either a versioned or non-versioned service
// endpoint URL from the given path.
func (n *Nova) endpointURL(version bool, path string) string {