Skip to content

APIError is suggested to be returned from stream.Recv() #302

Closed
@hophacker

Description

@hophacker

In the example below, it is very helpful to get err type other than err message at some cases. For example, when your quota exceeded, the raw response is

{
    "error": {
        "message": "You exceeded your current quota, please check your plan and billing details.",
        "type": "insufficient_quota",
        "param": null,
        "code": null
    }
}

This error will be returned by stream.Recv() and the error message is generated as error, You exceeded your current quota, please check your plan and billing details.". Error message is changeable due to many reasons, thus it is imprecise to determine that the account has no quota through it. Instead, error type is needed here since it is supposed to be hard to change.

So, an error object to return is needed here, a.k.a, APIError is suggested to be returned from stream.Recv()

		stream, err := tunnel.OpenaiClient.CreateChatCompletionStream(cc, req)
		if err != nil {
			return err
		}
		defer stream.Close()

		for {
			response, err := stream.Recv()
			if errors.Is(err, io.EOF) {
				fmt.Println("\nStream finished")
				return nil
			}

			if err != nil {
				return err
			}
			w.Write([]byte(response.Choices[0].Delta.Content))
			w.(http.Flusher).Flush()
		}
	}

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions