Implement fetch action, normalize error response params (#13)

This commit is contained in:
Maxim Baz
2018-04-17 00:18:05 +02:00
committed by GitHub
parent bd6ab67d3a
commit 8db942e669
7 changed files with 335 additions and 87 deletions

View File

@@ -52,6 +52,16 @@ func MakeListResponse() *ListResponse {
}
}
// FetchResponse a response format for the "fetch" request
type FetchResponse struct {
Contents string `json:"contents"`
}
// MakeFetchResponse initializes an empty fetch response
func MakeFetchResponse() *FetchResponse {
return &FetchResponse{}
}
// SendOk sends a success response to the browser extension in the predefined json format
func SendOk(data interface{}) {
send(&okResponse{
@@ -61,22 +71,16 @@ func SendOk(data interface{}) {
})
}
// SendError sends an error response to the browser extension in the predefined json format
func SendError(errorCode errors.Code, errorMsg string, extraParams *map[string]string) {
params := map[string]string{
"message": errorMsg,
}
if extraParams != nil {
for key, value := range *extraParams {
params[key] = value
}
}
// SendErrorAndExit sends an error response to the browser extension in the predefined json format and exits with the specified exit code
func SendErrorAndExit(errorCode errors.Code, params *map[errors.Field]string) {
send(&errorResponse{
Status: "error",
Code: errorCode,
Version: version.Code,
Params: params,
})
errors.ExitWithCode(errorCode)
}
func send(data interface{}) {