question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG][GO][Client] Returned err is never nil

See original GitHub issue

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What’s the actual output vs expected output?
Description

When calling any generated Execute() function, a non-nil error is always returned. Instead of using the usual Go idiom:

result, _, err := r.Execute()
if err != nil {
	/* deal with error */
}

I need to check the actual error message in order to detect errors:

result, _, err := r.Execute()
if err != nil && err.Error() != "" {
	/* deal with error */
}
openapi-generator version

c3220848 (current main branch)

Related issues/PRs

I believe this was introduced by #8137. Maybe it is intended behaviour. Care to weigh in here, @code-lucidal58?

Suggest a fix

Successful execution of *Execute() should not return a non-nil error.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:5
  • Comments:18 (12 by maintainers)

github_iconTop GitHub Comments

5reactions
cheshircommented, Jan 7, 2021

@code-lucidal58, the semantic of golang error will be broken. Some part of the community uses codegen to generate wrappers around the API clients for writing logs, metrics, traces etc. They highly depend on the error type. E.g. gowrap template for opentracing.

Let’s look closer to the idea of returning error type: If you care about readability – we can use a helper function to extract necessary data using type assertion. If you care about performance – I’m sure that single type assertion Is not the main performance issue in call processing.

It would be great to hear thoughts of the other community members.

3reactions
zippolytecommented, Jan 8, 2021

I’m in favor of reverting to the previous behaviour to keep the semantics of golang error.

I think the helper function is a good way to improve readability and avoid too many code duplications when dealing with errors. This is what is done in our terraform provider for instance, which uses a generated go client:

Cheers

Read more comments on GitHub >

github_iconTop Results From Across the Web

Golang returning nil does not return nil - Stack Overflow
It works when there are errors to print, but now it is causing a panic. Demo type Error struct { ErrString string }...
Read more >
Why Golang Nil Is Not Always Nil? Nil Explained - Code Fibers
The solution is simple - always use error interface when returning an error from function and never initialize an empty error variable that...
Read more >
Copypasting "if err!= nil {return err;} everywhere : r/golang
Just return err should almost never appear in code; at the least it should be return fmt.Errorf("failed to foo the bar: %s", err)...
Read more >
Returning nil custom error type - Getting Help - Go Forum
Below codes are about a function that returning nil custom error in two variations. Expectantly, these codes should always exit successfully ...
Read more >
Use Go if err != nil Idiom - YouTube
Use Go if err != nil Idiom ... (Exceptions will never be a thing in Go. ... It is also idiomatic to return...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found