Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Endpoint errors but returned error is nil #163

Open
joeyak opened this issue Dec 26, 2022 · 0 comments
Open

Endpoint errors but returned error is nil #163

joeyak opened this issue Dec 26, 2022 · 0 comments

Comments

@joeyak
Copy link
Contributor

joeyak commented Dec 26, 2022

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)
}
@joeyak joeyak changed the title Endpoint error returns Endpoint errors but returned error is nil Dec 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant