Skip to content

Commit

Permalink
Return a proper error on compile errors
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Dec 22, 2020
1 parent 39fcffb commit 7352e14
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
1 change: 0 additions & 1 deletion options.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ func (opts *Options) init() error {
// always refers to the same stylesheet.
//
// Load loads the canonicalized URL's content.
// TODO1 consider errors.
type ImportResolver interface {
CanonicalizeURL(url string) string
Load(canonicalizedURL string) string
Expand Down
36 changes: 34 additions & 2 deletions transpiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package godartsass

import (
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -88,13 +89,36 @@ type Result struct {
SourceMap string
}

// SassError is the error returned from Execute on compile errors.
type SassError struct {
Message string `json:"message"`
Span struct {
Text string `json:"text"`
Start struct {
Offset int `json:"offset"`
Column int `json:"column"`
} `json:"start"`
End struct {
Offset int `json:"offset"`
Column int `json:"column"`
} `json:"end"`
Context string `json:"context"`
} `json:"span"`
}

func (e SassError) Error() string {
return e.Message
}

// Close closes the stream to the embedded Dart Sass Protocol, which
// shuts down.
func (t *Transpiler) Close() error {
return t.conn.Close()
}

// Execute transpiles the string Source given in Args into CSS.
// If Dart Sass resturns a "compile failure", the error returned will be
// of type SassError..
func (t *Transpiler) Execute(args Args) (Result, error) {
var result Result

Expand Down Expand Up @@ -133,8 +157,16 @@ func (t *Transpiler) Execute(args Args) (Result, error) {
result.CSS = resp.Success.Css
result.SourceMap = resp.Success.SourceMap
case *embeddedsass.OutboundMessage_CompileResponse_Failure:
// TODO1 create a better error: offset, context etc.
return result, fmt.Errorf("compile failed: %s", resp.Failure.GetMessage())
asJson, err := json.Marshal(resp.Failure)
if err != nil {
return result, err
}
var sassErr SassError
err = json.Unmarshal(asJson, &sassErr)
if err != nil {
return result, err
}
return result, sassErr
default:
return result, fmt.Errorf("unsupported response type: %T", resp)
}
Expand Down
2 changes: 1 addition & 1 deletion transpiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (t testImportResolver) CanonicalizeURL(url string) string {
return ""
}

return filepath.Clean("file:/myproject/scss/" + url + "_myfile.scss")
return "file:/myproject/scss/" + url + "_myfile.scss"
}

func (t testImportResolver) Load(url string) string {
Expand Down

0 comments on commit 7352e14

Please sign in to comment.