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

instantiated error types not being generated/outputted #19

Closed
codeliger opened this issue Mar 6, 2023 · 3 comments
Closed

instantiated error types not being generated/outputted #19

codeliger opened this issue Mar 6, 2023 · 3 comments

Comments

@codeliger
Copy link

codeliger commented Mar 6, 2023

type ErrNotFound error

var (
	ErrFoodNotFound            ErrNotFound = errors.New("food not found")
	ErrCompanyNotFound           ErrNotFound = errors.New("company not found")
	ErrUserNotFound            ErrNotFound = errors.New("user not found")
)

output:

export type ErrNotFound = error;

I am trying to use this common pattern in go, but none of the instantiated errors are being generated
Is there a work around for this @gzuidhof

@gzuidhof
Copy link
Owner

gzuidhof commented Mar 6, 2023

Hmm there is no obvious way to do it, it's hard for Tygo to have some generic way of dealing with instantiated variables.

I would propose that the most straightforward way is to add some frontmatter manually, you can define frontmatter in the yaml file and it will be put into the output file.

It's not often I see raw error being serialized into Typescript/JSON, I don't even know what it looks like. I guess in the end it becomes a string? In that case, you could use something like the following as frontmatter:

export type error = string;

export const ErrFoodNotFound: error = "food not found";
export const ErrCompanyNotFound: error = "company not found";
export const ErrUserNotFound: error = "user not found";

Of course it's less than ideal to do it manually, but perhaps for your usecase it's fine?

@codeliger
Copy link
Author

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error
https://go.dev/blog/error-handling-and-go

In golang an error satisifes this interface

type error interface {
    Error() string
}

I was hoping I could at least create a struct called Error with that interface and assign it. But if I can't use var I will have the same issue.

@codeliger
Copy link
Author

I found a good Go article for anyone interested in this issue:
https://dave.cheney.net/tag/errors

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

2 participants