-
-
Notifications
You must be signed in to change notification settings - Fork 83
/
error.go
108 lines (93 loc) · 2.56 KB
/
error.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright (c) 2023, Cogent Core. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Based on https://github.com/hack-pad/hackpad
// Licensed under the Apache 2.0 License
//go:build js
package jsfs
import (
"fmt"
"io"
"log"
"os/exec"
"syscall"
"syscall/js"
"github.com/hack-pad/hackpadfs"
"github.com/pkg/errors"
)
// JsError converts the given error value into a JS error value.
func JSError(err error, message string, args ...js.Value) js.Value {
if err == nil {
return js.Null()
}
errMessage := errors.Wrap(err, message).Error()
for _, arg := range args {
errMessage += fmt.Sprintf("\n%v", arg)
}
return js.ValueOf(map[string]interface{}{
"message": js.ValueOf(errMessage),
"code": js.ValueOf(GetErrType(err, errMessage)),
})
}
// GetErrType returns the JS type of the given error.
func GetErrType(err error, debugMessage string) string {
if err, ok := err.(interface{ Code() string }); ok {
return err.Code()
}
if err := errors.Unwrap(err); err != nil {
return GetErrType(err, debugMessage)
}
if errno, ok := err.(syscall.Errno); ok {
switch errno {
case syscall.EBADF:
return "EBADF"
case syscall.ENOENT:
return "ENOENT"
case syscall.EEXIST:
return "EEXIST"
case syscall.EISDIR:
return "EISDIR"
case syscall.EPERM:
return "EPERM"
case syscall.EINVAL:
return "EINVAL"
default:
log.Printf("jsfs.GetErrType: got unknown syscall error number: (%d) %+v\n\n%s\n", errno, err, debugMessage)
return "EPERM"
}
}
switch err {
case io.EOF, exec.ErrNotFound:
return "ENOENT"
}
switch {
case errors.Is(err, hackpadfs.ErrClosed):
return "EBADF" // if it was already closed, then the file descriptor was invalid
case errors.Is(err, hackpadfs.ErrNotExist):
return "ENOENT"
case errors.Is(err, hackpadfs.ErrExist):
return "EEXIST"
case errors.Is(err, hackpadfs.ErrIsDir):
return "EISDIR"
case errors.Is(err, hackpadfs.ErrPermission):
return "EPERM"
default:
log.Printf("jsfs.GetErrType: got unknown error type: (%T) %+v\n\n%s\n", err, err, debugMessage)
return "EPERM"
}
}
func WrapError(err error, code string) error {
return &codeError{err, code}
}
type codeError struct {
error
code string
}
func (ce *codeError) Code() string { return ce.code }
func ErrBadFileNumber(fd uint64) error {
return WrapError(fmt.Errorf("bad file number %d", fd), "EBADF")
}
func ErrBadFile(identifier string) error {
return WrapError(fmt.Errorf("bad file %q", identifier), "EBADF")
}
var ErrNotDir = WrapError(errors.New("not a directory"), "ENOTDIR")