Skip to content

Commit cc91047

Browse files
committed
Move Error to a new libsasserrors package
To allow clients to reason (import) the errors without needing CGO.
1 parent e83cbce commit cc91047

File tree

3 files changed

+36
-22
lines changed

3 files changed

+36
-22
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright © 2022 Bjørn Erik Pedersen <[email protected]>.
2+
//
3+
// Use of this source code is governed by an MIT-style
4+
// license that can be found in the LICENSE file.
5+
6+
// Package libsasserrors holds the error types used by the libsass package.
7+
package libsasserrors
8+
9+
import (
10+
"encoding/json"
11+
"fmt"
12+
)
13+
14+
// JsonToError converts a JSON string to an error.
15+
func JsonToError(jsonstr string) (e Error) {
16+
_ = json.Unmarshal([]byte(jsonstr), &e)
17+
return
18+
}
19+
20+
// Error is a libsass error.
21+
type Error struct {
22+
Status int `json:"status"`
23+
Column int `json:"column"`
24+
File string `json:"file"`
25+
Line int `json:"line"`
26+
Message string `json:"message"`
27+
}
28+
29+
func (e Error) Error() string {
30+
return fmt.Sprintf("file %q, line %d, col %d: %s ", e.File, e.Line, e.Column, e.Message)
31+
}

libsass/transpiler.go

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright © 2020 Bjørn Erik Pedersen <[email protected]>.
1+
// Copyright © 2022 Bjørn Erik Pedersen <[email protected]>.
22
//
33
// Use of this source code is governed by an MIT-style
44
// license that can be found in the LICENSE file.
@@ -7,12 +7,11 @@
77
package libsass
88

99
import (
10-
"encoding/json"
11-
"fmt"
1210
"os"
1311
"strings"
1412

1513
"github.com/bep/golibsass/internal/libsass"
14+
"github.com/bep/golibsass/libsass/libsasserrors"
1615
)
1716

1817
type libsassTranspiler struct {
@@ -81,7 +80,7 @@ func (t libsassTranspiler) Execute(src string) (Result, error) {
8180
libsass.SassCompilerExecute(compiler)
8281

8382
if status := libsass.SassContextGetErrorStatus(ctx); status != 0 {
84-
return result, jsonToError(libsass.SassContextGetErrorJSON(ctx))
83+
return result, libsasserrors.JsonToError(libsass.SassContextGetErrorJSON(ctx))
8584
}
8685

8786
result.CSS = libsass.SassContextGetOutputString(ctx)
@@ -159,20 +158,3 @@ type SourceMapOptions struct {
159158
OmitURL bool
160159
EnableEmbedded bool
161160
}
162-
163-
func jsonToError(jsonstr string) (e Error) {
164-
_ = json.Unmarshal([]byte(jsonstr), &e)
165-
return
166-
}
167-
168-
type Error struct {
169-
Status int `json:"status"`
170-
Column int `json:"column"`
171-
File string `json:"file"`
172-
Line int `json:"line"`
173-
Message string `json:"message"`
174-
}
175-
176-
func (e Error) Error() string {
177-
return fmt.Sprintf("file %q, line %d, col %d: %s ", e.File, e.Line, e.Column, e.Message)
178-
}

libsass/transpiler_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"sync"
1414
"testing"
1515

16+
"github.com/bep/golibsass/libsass/libsasserrors"
1617
qt "github.com/frankban/quicktest"
1718
)
1819

@@ -85,7 +86,7 @@ func TestError(t *testing.T) {
8586
_, err = transpiler.Execute("\n\ndiv { color: $blue; }")
8687
c.Assert(err, qt.Not(qt.IsNil))
8788

88-
lerr := err.(Error)
89+
lerr := err.(libsasserrors.Error)
8990
c.Assert(lerr.Line, qt.Equals, 3)
9091
c.Assert(lerr.Column, qt.Equals, 14)
9192
c.Assert(lerr.Message, qt.Equals, `Undefined variable: "$blue".`)

0 commit comments

Comments
 (0)