3 Star 1 Fork 0

Gitee 极速下载/otto

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/robertkrimen/otto
克隆/下载
error.go 4.93 KB
一键复制 编辑 原始数据 按行查看 历史
Steven Hartland 提交于 2024-04-13 17:05 . chore: update ci versions (#519)
package otto
import (
"errors"
"fmt"
"github.com/robertkrimen/otto/file"
)
type exception struct {
value interface{}
}
func newException(value interface{}) *exception {
return &exception{
value: value,
}
}
func (e *exception) eject() interface{} {
value := e.value
e.value = nil // Prevent Go from holding on to the value, whatever it is
return value
}
type ottoError struct {
name string
message string
trace []frame
offset int
}
func (e ottoError) format() string {
if len(e.name) == 0 {
return e.message
}
if len(e.message) == 0 {
return e.name
}
return fmt.Sprintf("%s: %s", e.name, e.message)
}
func (e ottoError) formatWithStack() string {
str := e.format() + "\n"
for _, frm := range e.trace {
str += " at " + frm.location() + "\n"
}
return str
}
type frame struct {
fn interface{}
file *file.File
nativeFile string
callee string
nativeLine int
offset int
native bool
}
var nativeFrame = frame{}
type at int
func (fr frame) location() string {
str := "<unknown>"
switch {
case fr.native:
str = "<native code>"
if fr.nativeFile != "" && fr.nativeLine != 0 {
str = fmt.Sprintf("%s:%d", fr.nativeFile, fr.nativeLine)
}
case fr.file != nil:
if p := fr.file.Position(file.Idx(fr.offset)); p != nil {
path, line, column := p.Filename, p.Line, p.Column
if path == "" {
path = "<anonymous>"
}
str = fmt.Sprintf("%s:%d:%d", path, line, column)
}
}
if fr.callee != "" {
str = fmt.Sprintf("%s (%s)", fr.callee, str)
}
return str
}
// An Error represents a runtime error, e.g. a TypeError, a ReferenceError, etc.
type Error struct {
ottoError
}
// Error returns a description of the error
//
// TypeError: 'def' is not a function
func (e Error) Error() string {
return e.format()
}
// String returns a description of the error and a trace of where the
// error occurred.
//
// TypeError: 'def' is not a function
// at xyz (<anonymous>:3:9)
// at <anonymous>:7:1/
func (e Error) String() string {
return e.formatWithStack()
}
// GoString returns a description of the error and a trace of where the
// error occurred. Printing with %#v will trigger this behaviour.
func (e Error) GoString() string {
return e.formatWithStack()
}
func (e ottoError) describe(format string, in ...interface{}) string {
return fmt.Sprintf(format, in...)
}
func (e ottoError) messageValue() Value {
if e.message == "" {
return Value{}
}
return stringValue(e.message)
}
func (rt *runtime) typeErrorResult(throw bool) bool {
if throw {
panic(rt.panicTypeError())
}
return false
}
func newError(rt *runtime, name string, stackFramesToPop int, in ...interface{}) ottoError {
err := ottoError{
name: name,
offset: -1,
}
description := ""
length := len(in)
if rt != nil && rt.scope != nil {
curScope := rt.scope
for i := 0; i < stackFramesToPop; i++ {
if curScope.outer != nil {
curScope = curScope.outer
}
}
frm := curScope.frame
if length > 0 {
if atv, ok := in[length-1].(at); ok {
in = in[0 : length-1]
if curScope != nil {
frm.offset = int(atv)
}
length--
}
if length > 0 {
description, in = in[0].(string), in[1:]
}
}
limit := rt.traceLimit
err.trace = append(err.trace, frm)
if curScope != nil {
for curScope = curScope.outer; curScope != nil; curScope = curScope.outer {
if limit--; limit == 0 {
break
}
if curScope.frame.offset >= 0 {
err.trace = append(err.trace, curScope.frame)
}
}
}
} else if length > 0 {
description, in = in[0].(string), in[1:]
}
err.message = err.describe(description, in...)
return err
}
func (rt *runtime) panicTypeError(argumentList ...interface{}) *exception {
return &exception{
value: newError(rt, "TypeError", 0, argumentList...),
}
}
func (rt *runtime) panicReferenceError(argumentList ...interface{}) *exception {
return &exception{
value: newError(rt, "ReferenceError", 0, argumentList...),
}
}
func (rt *runtime) panicURIError(argumentList ...interface{}) *exception {
return &exception{
value: newError(rt, "URIError", 0, argumentList...),
}
}
func (rt *runtime) panicSyntaxError(argumentList ...interface{}) *exception {
return &exception{
value: newError(rt, "SyntaxError", 0, argumentList...),
}
}
func (rt *runtime) panicRangeError(argumentList ...interface{}) *exception {
return &exception{
value: newError(rt, "RangeError", 0, argumentList...),
}
}
func catchPanic(function func()) (err error) {
defer func() {
if caught := recover(); caught != nil {
if excep, ok := caught.(*exception); ok {
caught = excep.eject()
}
switch caught := caught.(type) {
case *Error:
err = caught
return
case ottoError:
err = &Error{caught}
return
case Value:
if vl := caught.object(); vl != nil {
if vl, ok := vl.value.(ottoError); ok {
err = &Error{vl}
return
}
}
err = errors.New(caught.string())
return
}
panic(caught)
}
}()
function()
return nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors/otto.git
git@gitee.com:mirrors/otto.git
mirrors
otto
otto
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385