Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions core/utils/req_helper/proxy_local/req_to_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,30 @@ func NewLocalClient(reqUrl, reqMethod string, body io.Reader, ctx *gin.Context)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("do request failed, err: %v", resp.Status)
}
bodyByte, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("read resp body from request failed, err: %v", err)
}

if resp.StatusCode == http.StatusUnauthorized {
return nil, fmt.Errorf("authentication failed: remote panel returned 401 Unauthorized, please check API key or credentials")
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("do request failed, status: %s", resp.Status)
}

contentType := resp.Header.Get("Content-Type")
if !strings.Contains(contentType, "application/json") && !strings.Contains(contentType, "text/json") {
preview := string(bodyByte)
if len(preview) > 200 {
preview = preview[:200]
}
return nil, fmt.Errorf("expected JSON response but got Content-Type: %s, body: %s", contentType, preview)
}

var respJson dto.Response
if err := json.Unmarshal(bodyByte, &respJson); err != nil {
return nil, fmt.Errorf("json umarshal resp data failed, err: %v", err)
return nil, fmt.Errorf("json unmarshal resp data failed, err: %v", err)
}
if respJson.Code != http.StatusOK {
return nil, errors.New(strings.ReplaceAll(respJson.Message, i18n.Get("ErrInternalServerKey"), ""))
Expand Down