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

Line and column numbers for syntax errors #24

Closed
kyleconroy opened this issue Aug 19, 2019 · 4 comments
Closed

Line and column numbers for syntax errors #24

kyleconroy opened this issue Aug 19, 2019 · 4 comments

Comments

@kyleconroy
Copy link

kyleconroy commented Aug 19, 2019

When parsing a invalid query, Parse returns an error string starting with "syntax error ...". Would it be possible to get the offset of the syntax error, similar to SyntaxError type in the json package?

Thanks again for the awesome package :) I'm using it to power my new project https://github.com/kyleconroy/sqlc

@Davincible
Copy link

Would be really nice to get this working. Then the results can be used as LSP

@StevenACoffman
Copy link

@kyleconroy The parser.Error type has an Error() method that only includes the message, but that same parser.Error type has all the other fields public.

So it looks like a PgQueryError error has:

		error = malloc(sizeof(PgQueryError));
		error->message   = strdup(error_data->message);
		error->filename  = strdup(error_data->filename);
		error->funcname  = strdup(error_data->funcname);
		error->context   = NULL;
		error->lineno    = error_data->lineno;
		error->cursorpos = error_data->cursorpos;

		result.error = error;	

Which then is read by

func newPgQueryError(errC *C.PgQueryError) *Error {
	err := &Error{
		Message:   C.GoString(errC.message),
		Lineno:    int(errC.lineno),
		Cursorpos: int(errC.cursorpos),
	}
	if errC.funcname != nil {
		err.Funcname = C.GoString(errC.funcname)
	}
	if errC.filename != nil {
		err.Filename = C.GoString(errC.filename)
	}
	if errC.context != nil {
		err.Context = C.GoString(errC.context)
	}
	return err
}

So the parser.Error should be fully populated:

type Error struct {
	Message   string // exception message
	Funcname  string // source function of exception (e.g. SearchSysCache)
	Filename  string // source of exception (e.g. parse.l)
	Lineno    int    // source of exception (e.g. 104)
	Cursorpos int    // char in query at which exception occurred
	Context   string // additional context (optional, can be NULL)
}

@StevenACoffman
Copy link

StevenACoffman commented May 19, 2023

I believe #76 fixes this issue and this issue can now be closed, unless you are waiting until this is part of a release (as v4.2.0 from Feb 8 does not contain this change from Mar 10 ).

By the way, thanks very much for this work! This is very much appreciated, as is the rest of your extremely useful library!

@lfittl
Copy link
Member

lfittl commented May 19, 2023

@StevenACoffman Good catch - yes, I think this can be closed. I'm in the process of doing a 4.2.1 release, so this should also be officially released soon.

@lfittl lfittl closed this as completed May 19, 2023
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

4 participants