Skip to content

Conversation

casualjim
Copy link
Member

@casualjim casualjim commented Feb 5, 2022

Adds the same method sets to runtime.APIError and the generated client responses. There is also a convenience interface runtime.ClientResponseStatus to runtime

// IsSuccess returns true when this elapse o k response returns a 2xx status code
func (o *APIError) IsSuccess() bool {
	return o.Code/100 == 2
}

// IsRedirect returns true when this elapse o k response returns a 3xx status code
func (o *APIError) IsRedirect() bool {
	return o.Code/100 == 3
}

// IsClientError returns true when this elapse o k response returns a 4xx status code
func (o *APIError) IsClientError() bool {
	return o.Code/100 == 4
}

// IsServerError returns true when this elapse o k response returns a 5xx status code
func (o *APIError) IsServerError() bool {
	return o.Code/100 == 5
}

// IsCode returns true when this elapse o k response returns a 4xx status code
func (o *APIError) IsCode(code int) bool {
	return o.Code == code
}

So you should be able to use it as:

func IsClientError(err error) bool {
	if rerr, ok := err.(runtime.ClientResponseStatus); ok {
            return rerr.IsClientError() 
        }
	return false
}

closes #2706

@codecov
Copy link

codecov bot commented Feb 5, 2022

Codecov Report

Merging #2708 (1cb2023) into master (13f57ef) will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #2708   +/-   ##
=======================================
  Coverage   82.92%   82.92%           
=======================================
  Files          55       55           
  Lines        9543     9543           
=======================================
  Hits         7914     7914           
  Misses       1116     1116           
  Partials      513      513           

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 13f57ef...1cb2023. Read the comment docs.

@casualjim casualjim merged commit c4d4cfe into master Feb 5, 2022
@casualjim casualjim deleted the issue-2706 branch February 5, 2022 19:33
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

Successfully merging this pull request may close these issues.

Call for transparent HTTP status codes in generated struct
2 participants