Skip to content
Snippets Groups Projects
Commit 7e27bf72 authored by Jono Wenger's avatar Jono Wenger
Browse files

Move errors to own file

parent 6663cc6a
No related branches found
No related tags found
2 merge requests!60Revert "Fail a test to be sure it works",!5XX-4050 / SendE2e test
////////////////////////////////////////////////////////////////////////////////
// Copyright © 2022 xx network SEZC //
// //
// Use of this source code is governed by a license that can be found in the //
// LICENSE file //
////////////////////////////////////////////////////////////////////////////////
package utils
import (
"fmt"
"syscall/js"
)
// JsError converts the error to a Javascript Error.
func JsError(err error) js.Value {
return Error.New(err.Error())
}
// JsTrace converts the error to a Javascript Error that includes the error's
// stack trace.
func JsTrace(err error) js.Value {
return Error.New(fmt.Sprintf("%+v", err))
}
// Throw function stub to throws Javascript exceptions. The exception must be
// one of the defined Exception below. Any other error types will result in an
// error.
func Throw(exception Exception, err error) {
throw(exception, fmt.Sprintf("%+v", err))
}
func throw(exception Exception, message string)
// Exception are the possible Javascript error types that can be thrown.
type Exception string
const (
// EvalError occurs when error has occurred in the eval() function.
//
// Deprecated: This exception is not thrown by JavaScript anymore, however
// the EvalError object remains for compatibility.
EvalError Exception = "EvalError"
// RangeError occurs when a numeric variable or parameter is outside its
// valid range.
RangeError Exception = "RangeError"
// ReferenceError occurs when a variable that does not exist (or hasn't yet
// been initialized) in the current scope is referenced.
ReferenceError Exception = "ReferenceError"
// SyntaxError occurs when trying to interpret syntactically invalid code.
SyntaxError Exception = "SyntaxError"
// TypeError occurs when an operation could not be performed, typically (but
// not exclusively) when a value is not of the expected type.
//
// A TypeError may be thrown when:
//
// - an operand or argument passed to a function is incompatible with the
// type expected by that operator or function; or
// - when attempting to modify a value that cannot be changed; or
// - when attempting to use a value in an inappropriate way.
TypeError Exception = "TypeError"
// URIError occurs when a global URI handling function was used in a wrong
// way.
URIError Exception = "URIError"
)
////////////////////////////////////////////////////////////////////////////////
// Copyright © 2022 xx foundation //
// //
// Use of this source code is governed by a license that can be found in the //
// LICENSE file. //
////////////////////////////////////////////////////////////////////////////////
package utils
import (
"github.com/pkg/errors"
"syscall/js"
"testing"
)
func TestJsError(t *testing.T) {
err := errors.Errorf("test error")
jsError := JsError(err)
t.Logf("%+v", jsError)
t.Logf("%+v", jsError.String())
t.Logf("%+v", js.Error{Value: jsError})
}
func TestJsTrace(t *testing.T) {
}
func TestThrow(t *testing.T) {
}
func Test_throw(t *testing.T) {
}
......@@ -18,15 +18,15 @@ package utils
import (
"encoding/json"
"fmt"
"github.com/pkg/errors"
jww "github.com/spf13/jwalterweatherman"
"syscall/js"
)
var (
promiseConstructor = js.Global().Get("Promise")
Uint8Array = js.Global().Get("Uint8Array")
Error = js.Global().Get("Error")
Promise = js.Global().Get("Promise")
Uint8Array = js.Global().Get("Uint8Array")
)
// CopyBytesToGo copies the Uint8Array stored in the js.Value to []byte. This is
......@@ -73,26 +73,6 @@ func JsonToJS(src []byte) js.Value {
return js.ValueOf(inInterface)
}
// JsError converts the error to a Javascript Error.
func JsError(err error) js.Value {
errorConstructor := js.Global().Get("Error")
return errorConstructor.New(err.Error())
}
// JsTrace converts the error to a Javascript Error that includes the error's
// stack trace.
func JsTrace(err error) js.Value {
errorConstructor := js.Global().Get("Error")
return errorConstructor.New(fmt.Sprintf("%+v", err))
}
// Throw function stub to throws Javascript exceptions. The exception must be
// one of the defined Exception below. Any other error types will result in an
// error.
func Throw(exception Exception, err error) {
throw(exception, fmt.Sprintf("%+v", err))
}
type PromiseFn func(resolve, reject func(args ...interface{}) js.Value)
// CreatePromise creates a Javascript promise to return the value of a blocking
......@@ -109,44 +89,5 @@ func CreatePromise(f PromiseFn) interface{} {
})
// Create and return the Promise object
return promiseConstructor.New(handler)
return Promise.New(handler)
}
func throw(exception Exception, message string)
// Exception are the possible Javascript error types that can be thrown.
type Exception string
const (
// EvalError occurs when error has occurred in the eval() function.
//
// Deprecated: This exception is not thrown by JavaScript anymore, however
// the EvalError object remains for compatibility.
EvalError Exception = "EvalError"
// RangeError occurs when a numeric variable or parameter is outside its
// valid range.
RangeError Exception = "RangeError"
// ReferenceError occurs when a variable that does not exist (or hasn't yet
// been initialized) in the current scope is referenced.
ReferenceError Exception = "ReferenceError"
// SyntaxError occurs when trying to interpret syntactically invalid code.
SyntaxError Exception = "SyntaxError"
// TypeError occurs when an operation could not be performed, typically (but
// not exclusively) when a value is not of the expected type.
//
// A TypeError may be thrown when:
//
// - an operand or argument passed to a function is incompatible with the
// type expected by that operator or function; or
// - when attempting to modify a value that cannot be changed; or
// - when attempting to use a value in an inappropriate way.
TypeError Exception = "TypeError"
// URIError occurs when a global URI handling function was used in a wrong
// way.
URIError Exception = "URIError"
)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment