This maybe a noob question but I've been googling for a while.
I am running in a for loop, for each loop a different api request
jiraClient, err := jira.NewClient(nil, *jiraURL)
if err != nil {
panic(err)
}
jiraClient.Authentication.SetBasicAuth(*jiraUser, string(jiraPass))
reader := csv.NewReader(*file)
CSVData, err := reader.ReadAll()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
lineCount := 1
for _, line := range data {
//reading CSV
//this returns 200 for all users found but 404 for not found
user, resp, _ := jiraClient.User.Get(user)
if resp.StatusCode == 200 {
fmt.Printf("Changed %s to %s
", user.Name, data.user)
} else {
// have tried continue, return
}
}
Every time it gets to a 404 response, the program fails and exits
I've tried a try and catch with no luck.
How can i have it continue?
This question leaks details that because it earned so much down-votes. I assumed that we are dealing with a Get method of UserService in go-jira.
The Get
method returns error:
func (s *UserService) Get(username string) (*User, *Response, error)
You are omitting error check and pulling it to a blank identifier. Note that if an error returned with a non-nil value then the *User
is nil
.
resp, err := s.client.Do(req, user)
if err != nil {
return nil, resp, err
}
And if even I incorrectly assumed a lib which you are using it is a Go way to handle the situation. The proper way is to check error value and do what you need but not wait until your program face nil pointer dereference.
user, resp, err := jiraClient.User.Get()
if err != nil {
// Do something clever...
}
Furthermore error
is an interface. So error could contain some useful data or be an object of a concrete type.
An error variable represents any value that can describe itself as a string. Here is the interface's declaration:
type error interface {
Error() string
}