Skip to content

Endpoint errors but returned error is nil #163

@joeyak

Description

@joeyak

I ran into an issue where I was calling GetUsers(...) and twitch was returning an error, but the helix library returned an empty error, so I didn't handle it right away.

In my program, I'm wrapping all error responses before I check for an error

func GetHelixError(err error, resp helix.ResponseCommon) error {
	if resp.ErrorStatus == 0 && err == nil {
		return nil
	}

	if err != nil {
		return err
	}

	return fmt.Errorf("error %s with status %d: %s", resp.Error, resp.ErrorStatus, resp.ErrorMessage)
}

// Example call
userResp, err := tokenClient.GetUsers(&helix.UsersParams{Logins: []string{strings.ToLower(botName)}})
err = service.GetHelixError(err, userResp.ResponseCommon)
if err != nil {
	return fmt.Errorf("could not get user info for bot %s: %w", botName, err)
}

Is the expected way to always check resp.ErrorStatus for it being set. This feels more a limitation of twitch not using proper status codes... but it's what we have to work with.

One thing that could be done, but it would require a lot of refactoring, is add a wrap function. Wrapping it on TwitchResponseError would allow a check with errors.Is(err, helix.TwitchResponseError)

var TwitchResponseError = errors.New("twitch returned an error")

func pullErrorFromResponse(resp ResponseCommon) error {
	if resp.ErrorStatus == 0 {
		return nil
	}

	return fmt.Errorf("error %s occured with status %d and message %s: %w", resp.Error, resp.ErrorStatus, resp.ErrorMessage, err)
}

// elsewhere
func (c *Client) GetStreams(params *StreamsParams) (*StreamsResponse, error) {
	resp, err := c.get("/streams", &ManyStreams{}, params)
	if err != nil {
		return nil, err
	}

	streams := &StreamsResponse{}
	resp.HydrateResponseCommon(&streams.ResponseCommon)
	streams.Data.Streams = resp.Data.(*ManyStreams).Streams
	streams.Data.Pagination = resp.Data.(*ManyStreams).Pagination

	// refactor is here \/
	return streams, pullErrorFromResponse(resp.ResponseCommon)
}

Metadata

Metadata

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions