Skip to content

Commit

Permalink
Add parse error message
Browse files Browse the repository at this point in the history
  • Loading branch information
杨赫然 committed Sep 12, 2024
1 parent a9c65a5 commit dcc4822
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
23 changes: 21 additions & 2 deletions fileserver/utils/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package utils

import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -36,8 +37,8 @@ func HttpCommon(method, url string, header map[string][]string, reader io.Reader
defer rsp.Body.Close()

if rsp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(rsp.Body)
return rsp.StatusCode, body, fmt.Errorf("bad response %d for %s", rsp.StatusCode, url)
errMsg := parseErrorMessage(rsp.Body)
return rsp.StatusCode, errMsg, fmt.Errorf("bad response %d for %s", rsp.StatusCode, url)
}

body, err := io.ReadAll(rsp.Body)
Expand All @@ -47,3 +48,21 @@ func HttpCommon(method, url string, header map[string][]string, reader io.Reader

return http.StatusOK, body, nil
}

func parseErrorMessage(r io.Reader) []byte {
body, err := io.ReadAll(r)
if err != nil {
return nil
}
var objs map[string]string
err = json.Unmarshal(body, &objs)
if err != nil {
return body
}
errMsg, ok := objs["error_msg"]
if ok {
return []byte(errMsg)
}

return body
}
33 changes: 32 additions & 1 deletion server/http-tx-mgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,37 @@ parse_share_link_info (const char *rsp_content, int rsp_size)
return info;
}

char *
parse_error_message (const char *rsp_content, int rsp_size)
{
json_t *object;
json_error_t jerror;
const char *err_msg = NULL;
char *ret = NULL;

if (!rsp_content) {
return NULL;
}

object = json_loadb (rsp_content, rsp_size, 0, &jerror);
if (!object) {
ret = g_strdup (rsp_content);
return ret;
}

err_msg = json_object_get_string_member (object, "error_msg");
if (!err_msg) {
ret = g_strdup (rsp_content);
goto out;
}
ret = g_strdup (err_msg);

out:
json_decref (object);

return ret;
}

SeafileShareLinkInfo *
http_tx_manager_query_share_link_info (const char *token, const char *cookie, const char *type, int *status, char **err_msg)
{
Expand Down Expand Up @@ -611,7 +642,7 @@ http_tx_manager_query_share_link_info (const char *token, const char *cookie, co

*status = rsp_status;
if (rsp_status != HTTP_OK) {
*err_msg = g_strdup (rsp_content);
*err_msg = parse_error_message (rsp_content, rsp_size);
goto out;
}

Expand Down

0 comments on commit dcc4822

Please sign in to comment.