Skip to content

Commit

Permalink
Issue #46: now is set if the Type is ApiErrorFile or ApiErrorSyntax
Browse files Browse the repository at this point in the history
  • Loading branch information
yuin committed Sep 5, 2015
1 parent d08b46c commit 45ed99f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion _glua-tests/base.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local ok, msg = pcall(function()
dofile("notexist")
end)
assert(not ok and string.find(msg, "cannot open notexist"))
assert(not ok and string.find(msg, "open.*notexist.*cannot.*"))

local ok, msg = pcall(function()
assert(getfenv(2) == _G)
Expand Down
2 changes: 1 addition & 1 deletion auxlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ func (ls *LState) LoadFile(path string) (*LFunction, error) {
file, err = os.Open(path)
defer file.Close()
if err != nil {
return nil, newApiErrorS(ApiErrorFile, fmt.Sprintf("cannot open %v: %v", path, err.Error()))
return nil, newApiErrorE(ApiErrorFile, err)
}
reader = file
}
Expand Down
14 changes: 11 additions & 3 deletions state.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,22 @@ type ApiError struct {
Type ApiErrorType
Object LValue
StackTrace string
// Underlying error. This attribute is set only if the Type is ApiErrorFile or ApiErrorSyntax
Cause error
}

func newApiError(code ApiErrorType, object LValue) *ApiError {
return &ApiError{code, object, ""}
return &ApiError{code, object, "", nil}
}

func newApiErrorS(code ApiErrorType, message string) *ApiError {
return newApiError(code, LString(message))
}

func newApiErrorE(code ApiErrorType, err error) *ApiError {
return &ApiError{code, LString(err.Error()), "", err}
}

func (e *ApiError) Error() string {
if len(e.StackTrace) > 0 {
return fmt.Sprintf("%s\n%s", e.Object.String(), e.StackTrace)
Expand Down Expand Up @@ -1194,10 +1200,12 @@ func (ls *LState) ToThread(n int) *LState {

/* error & debug operations {{{ */

// This function is equivalent to luaL_error( http://www.lua.org/manual/5.1/manual.html#luaL_error ).
func (ls *LState) RaiseError(format string, args ...interface{}) {
ls.raiseError(1, format, args...)
}

// This function is equivalent to lua_error( http://www.lua.org/manual/5.1/manual.html#lua_error ).
func (ls *LState) Error(lv LValue, level int) {
if str, ok := lv.(LString); ok {
ls.raiseError(level, string(str))
Expand Down Expand Up @@ -1470,11 +1478,11 @@ func (ls *LState) Register(name string, fn LGFunction) {
func (ls *LState) Load(reader io.Reader, name string) (*LFunction, error) {
chunk, err := parse.Parse(reader, name)
if err != nil {
return nil, newApiErrorS(ApiErrorSyntax, err.Error())
return nil, newApiErrorE(ApiErrorSyntax, err)
}
proto, err := Compile(chunk, name)
if err != nil {
return nil, newApiErrorS(ApiErrorSyntax, err.Error())
return nil, newApiErrorE(ApiErrorSyntax, err)
}
return newLFunctionL(proto, ls.currentEnv(), 0), nil
}
Expand Down

0 comments on commit 45ed99f

Please sign in to comment.