mirror of
https://github.com/foomo/contentserver.git
synced 2025-10-16 12:25:44 +00:00
handlerequest cleanup
This commit is contained in:
parent
42adb6a25a
commit
757c310f8d
@ -13,17 +13,18 @@ import (
|
|||||||
|
|
||||||
func handleRequest(r *repo.Repo, handler Handler, jsonBytes []byte) (replyBytes []byte, err error) {
|
func handleRequest(r *repo.Repo, handler Handler, jsonBytes []byte) (replyBytes []byte, err error) {
|
||||||
|
|
||||||
var reply interface{}
|
var (
|
||||||
var apiErr error
|
reply interface{}
|
||||||
var jsonErr error
|
apiErr error
|
||||||
|
jsonErr error
|
||||||
processIfJSONIsOk := func(err error, processingFunc func()) {
|
processIfJSONIsOk = func(err error, processingFunc func()) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
jsonErr = err
|
jsonErr = err
|
||||||
return
|
return
|
||||||
|
}
|
||||||
|
processingFunc()
|
||||||
}
|
}
|
||||||
processingFunc()
|
)
|
||||||
}
|
|
||||||
|
|
||||||
switch handler {
|
switch handler {
|
||||||
case HandlerGetURIs:
|
case HandlerGetURIs:
|
||||||
@ -56,28 +57,31 @@ func handleRequest(r *repo.Repo, handler Handler, jsonBytes []byte) (replyBytes
|
|||||||
errorResponse := responses.NewError(1, "unknown handler")
|
errorResponse := responses.NewError(1, "unknown handler")
|
||||||
reply = errorResponse
|
reply = errorResponse
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// check for errors and set reply
|
||||||
if jsonErr != nil {
|
if jsonErr != nil {
|
||||||
err = jsonErr
|
|
||||||
log.Error(" could not read incoming json:", jsonErr)
|
log.Error(" could not read incoming json:", jsonErr)
|
||||||
errorResponse := responses.NewError(2, "could not read incoming json "+jsonErr.Error())
|
err = jsonErr
|
||||||
reply = errorResponse
|
reply = responses.NewError(2, "could not read incoming json "+jsonErr.Error())
|
||||||
} else if apiErr != nil {
|
} else if apiErr != nil {
|
||||||
log.Error(" an API error occured:", apiErr)
|
log.Error(" an API error occured:", apiErr)
|
||||||
err = apiErr
|
err = apiErr
|
||||||
reply = responses.NewError(3, "internal error "+apiErr.Error())
|
reply = responses.NewError(3, "internal error "+apiErr.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
return encodeReply(reply)
|
return encodeReply(reply)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// encodeReply takes an interface and encodes it as JSON
|
||||||
|
// it returns the resulting JSON and a marshalling error
|
||||||
func encodeReply(reply interface{}) (replyBytes []byte, err error) {
|
func encodeReply(reply interface{}) (replyBytes []byte, err error) {
|
||||||
encodedBytes, jsonReplyErr := json.MarshalIndent(map[string]interface{}{
|
|
||||||
|
// @TODO: why use marshal indent here???
|
||||||
|
replyBytes, err = json.MarshalIndent(map[string]interface{}{
|
||||||
"reply": reply,
|
"reply": reply,
|
||||||
}, "", " ")
|
}, "", " ")
|
||||||
if jsonReplyErr != nil {
|
if err != nil {
|
||||||
err = jsonReplyErr
|
log.Error(" could not encode reply " + fmt.Sprint(err))
|
||||||
log.Error(" could not encode reply " + fmt.Sprint(jsonReplyErr))
|
|
||||||
} else {
|
|
||||||
replyBytes = encodedBytes
|
|
||||||
}
|
}
|
||||||
return replyBytes, err
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user