Skip to content

Commit

Permalink
feat: error handling added
Browse files Browse the repository at this point in the history
  • Loading branch information
nurali-techie committed Feb 1, 2025
1 parent 300b4f3 commit 320127a
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 7 deletions.
18 changes: 13 additions & 5 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,7 @@ func (c *Client) do(chatReq *request.ChatCompletionsRequest) (io.ReadCloser, err

if resp.StatusCode != 200 {
defer resp.Body.Close()
errMsg, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return nil, errors.New(string(errMsg))
return nil, processError(resp.Body, resp.StatusCode)
}

return resp.Body, nil
Expand All @@ -186,3 +182,15 @@ func setDefaultHeaders(req *http.Request, apiKey string) {
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
}

func processError(respBody io.Reader, statusCode int) error {
errBody, err := io.ReadAll(respBody)
if err != nil {
return err
}
errResp, err := internal.ParseError(errBody)
if err != nil {
return fmt.Errorf("err: %s; http_status_code=%d", errBody, statusCode)
}
return fmt.Errorf("err: %s; http_status_code=%d", errResp.Error.Message, statusCode)
}
22 changes: 22 additions & 0 deletions internal/error_parser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package internal

import (
"encoding/json"
)

type ErrorResponse struct {
Error *Error `json:"error"`
}

type Error struct {
Message string `json:"message"`
Type string `json:"type"`
Param any `json:"param"`
Code string `json:"code"`
}

func ParseError(errBody []byte) (*ErrorResponse, error) {
errResp := &ErrorResponse{}
err := json.Unmarshal(errBody, errResp)
return errResp, err
}
40 changes: 38 additions & 2 deletions notes.txt → notes.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,42 @@
** TODO
### Error handling
- error-code: https://api-docs.deepseek.com/quick_start/error_codes

** Sample Payload
code=401
```
{
"error": {
"message": "Authentication Fails (no such user)",
"type": "authentication_error",
"param": null,
"code": "invalid_request_error"
}
}
```

code=400
```
{
"error": {
"message": "Invalid frequency_penalty value, the valid range of frequency_penalty is [-2, 2]",
"type": "invalid_request_error",
"param": null,
"code": "invalid_request_error"
}
}
```

code=422 (when model not set)
```
Failed to deserialize the JSON body into the target type: missing field `model` at line 1 column 164
```

### Rate limit
- rate-limit: https://api-docs.deepseek.com/quick_start/rate_limit
- Non-streaming requests: Continuously return empty lines
- Streaming requests: Continuously return SSE keep-alive comments (: keep-alive)

### Sample Payload
```
{
"messages": [
{
Expand Down Expand Up @@ -47,3 +82,4 @@
"logprobs": false,
"top_logprobs": null
}
```

0 comments on commit 320127a

Please sign in to comment.