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

Improve API (sourceRoot option and importer errors) #915

Merged
merged 2 commits into from
Mar 8, 2015

Conversation

mgreter
Copy link
Contributor

@mgreter mgreter commented Mar 3, 2015

Addresses #880 (//CC @chriseppstein)

I opted to extend the internal struct to also carry error options.
To return a error from custom importers you need to:

char* message = SvPV_nolen(ERRSV);
incs = sass_make_import_list(1);
incs[0] = sass_make_import_entry(0, 0, 0);
sass_import_set_error(incs[0], message, line, col);

It will then throw an error as with regular imports that fail.

@am11
Copy link
Contributor

am11 commented Mar 3, 2015

👍 💯

@am11
Copy link
Contributor

am11 commented Mar 3, 2015

@mgreter, since the implementer doesn't have any context to resolve line and column information in importer, wouldn't it make sense that libsass handle those two at its own and just accept the error message?

@mgreter
Copy link
Contributor Author

mgreter commented Mar 5, 2015

The implementor could have a context, like testing if the content it is loading conforms to the rules it expects. IMO it could give more details where the actual error occurred, so I'm not really convinced to remove it. But you may be right that it is a bit over engineered. I guess it's no harm in leaving it in for now?

@mgreter mgreter added this to the 3.2 milestone Mar 5, 2015
@mgreter mgreter self-assigned this Mar 6, 2015
@drewwells
Copy link
Contributor

It works, awesome @mgreter !

So I'm trying to understand your discussion about context. Right now, libsass expects the implementor passes along the error line number/column. Would it be possible to provide error line number of where the @import line is? If that's already being passed, apologies for missing that.

I'm asking if sass_import_set_error is only passed a import list entry and a message (really no message would be fine with me "import not found: {name}" is all I will be putting in there. Here's the change I'm asking about:

    C.sass_import_set_error(ent, errMessage)

mgreter added 2 commits March 7, 2015 16:16
Allow custom importer to return an error. We just extend
the Import_Entry struct with error flags, since we can
re-use at least the path from the existing object.
@mgreter mgreter force-pushed the feature/improved-api branch from 0f72f88 to 29d19d8 Compare March 7, 2015 15:17
@mgreter
Copy link
Contributor Author

mgreter commented Mar 7, 2015

I added a switch, so if you set line and col to -1 (string::npos), it will throw the error on behalf of the parent context. This way you can just set the error message and leave the rest to libsass.

C.sass_import_set_error(ent, errMessage, -1, -1)

@drewwells
Copy link
Contributor

Can size_t be negative? I'm getting an error assigning -1.

constant -1 overflows C.size_t

@mgreter
Copy link
Contributor Author

mgreter commented Mar 7, 2015

Then you probably need to use string::npos, actually it is the biggest number possible, but mostly -1 should overflow to the biggest number (AFAIK) 😉 seems to work in C ...

@am11
Copy link
Contributor

am11 commented Mar 7, 2015

@drewwells,

really no message would be fine with me "import not found: {name}"

I think its not mandatory to return custom importer error for implementer. If you don't need the customized error, you can exclude it and libsass will fallback to its default missing-import error.

@drewwells
Copy link
Contributor

string::npos is C++, I used C.UINTMAX_MAX to get -1 in a size_t (in case anybody else comes across the same situation).

@am11 the error isn't mandatory, but returning any non-empty entry list uses that as the missing import. Returning an empty list or size 1 with nothing in it, results in a runtime exception. So I'm returning an error to override the blank import entry that seems to be required to do this.

Here's the code, if there's a better way to do it: https://github.com/wellington/wellington/blob/importer/context/export.go#L41

@mgreter
Copy link
Contributor Author

mgreter commented Mar 7, 2015

You could return a list with one entry, where you only set the filename (no data). Libsass will then try to load that file through it's loading code, which would emit the error @am11 probably meant. But IMO you should only do that if you are sure the file does not exist, otherwise it could have strange side effects!

@drewwells
Copy link
Contributor

I'm not great at C, what do I pass to sass_make_import_entry() to do that?
I'm currently passing empty char* but that is succeeding as a successful
import of an empty file.

On Sat, Mar 7, 2015 at 12:03 PM Marcel Greter [email protected]
wrote:

You could return a list with one entry, where you only set the filename
(no data). Libsass will then try to load that file through it's standard
loading code, which would emit the error @am11 https://github.com/am11
probably meant. But IMO you should only do that if you are sure the file
does not exist, otherwise it could have strange side effects!


Reply to this email directly or view it on GitHub
#915 (comment).

@mgreter
Copy link
Contributor Author

mgreter commented Mar 7, 2015

sass_make_import_entry("non_existing_file", 0, 0);

@drewwells
Copy link
Contributor

Thanks for your patience, it's working great. Sending nil did the expected behavior

error > stdin:1
    file to import not found or unreadable: non_existing_file

@mgreter mgreter merged commit 29d19d8 into sass:master Mar 8, 2015
@mgreter mgreter deleted the feature/improved-api branch April 6, 2015 17:14
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.

3 participants