2017-01-28 22:39:52 +08:00
|
|
|
package eval
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2017-01-29 10:29:03 +08:00
|
|
|
"strconv"
|
|
|
|
"syscall"
|
2017-08-31 02:52:27 +08:00
|
|
|
"unsafe"
|
2017-01-28 22:39:52 +08:00
|
|
|
|
2021-01-27 09:28:38 +08:00
|
|
|
"src.elv.sh/pkg/diag"
|
|
|
|
"src.elv.sh/pkg/eval/vals"
|
|
|
|
"src.elv.sh/pkg/parse"
|
2021-05-04 05:17:46 +08:00
|
|
|
"src.elv.sh/pkg/persistent/hash"
|
2017-01-28 22:39:52 +08:00
|
|
|
)
|
|
|
|
|
2021-01-08 08:56:45 +08:00
|
|
|
// Exception represents exceptions. It is both a Value accessible to Elvish
|
|
|
|
// code, and can be returned by methods like like (*Evaler).Eval.
|
|
|
|
type Exception interface {
|
|
|
|
error
|
|
|
|
diag.Shower
|
|
|
|
Reason() error
|
|
|
|
StackTrace() *StackTrace
|
|
|
|
// This is not strictly necessary, but it makes sure that there is only one
|
|
|
|
// implementation of Exception, so that the compiler may de-virtualize this
|
|
|
|
// interface.
|
|
|
|
isException()
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewException creates a new Exception.
|
|
|
|
func NewException(reason error, stackTrace *StackTrace) Exception {
|
|
|
|
return &exception{reason, stackTrace}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implementation of the Exception interface.
|
|
|
|
type exception struct {
|
|
|
|
reason error
|
|
|
|
stackTrace *StackTrace
|
2017-01-28 22:39:52 +08:00
|
|
|
}
|
|
|
|
|
2020-09-03 12:54:32 +08:00
|
|
|
// StackTrace represents a stack trace as a linked list of diag.Context. The
|
|
|
|
// head is the innermost stack.
|
|
|
|
//
|
|
|
|
// Since pipelines can call multiple functions in parallel, all the StackTrace
|
|
|
|
// nodes form a DAG.
|
|
|
|
type StackTrace struct {
|
|
|
|
Head *diag.Context
|
|
|
|
Next *StackTrace
|
2020-03-29 21:53:34 +08:00
|
|
|
}
|
|
|
|
|
2021-01-08 08:56:45 +08:00
|
|
|
// Reason returns the Reason field if err is an Exception. Otherwise it returns
|
2018-10-09 21:21:26 +08:00
|
|
|
// err itself.
|
2020-09-05 04:04:58 +08:00
|
|
|
func Reason(err error) error {
|
2021-01-08 08:56:45 +08:00
|
|
|
if exc, ok := err.(*exception); ok {
|
|
|
|
return exc.reason
|
2018-10-09 21:21:26 +08:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-01-08 08:56:45 +08:00
|
|
|
// OK is a pointer to a special value of Exception that represents the absence
|
|
|
|
// of exception.
|
|
|
|
var OK = &exception{}
|
|
|
|
|
|
|
|
func (exc *exception) isException() {}
|
|
|
|
|
|
|
|
func (exc *exception) Reason() error { return exc.reason }
|
|
|
|
|
|
|
|
func (exc *exception) StackTrace() *StackTrace { return exc.stackTrace }
|
2017-01-29 10:29:03 +08:00
|
|
|
|
2019-04-19 05:15:34 +08:00
|
|
|
// Error returns the message of the cause of the exception.
|
2021-01-08 08:56:45 +08:00
|
|
|
func (exc *exception) Error() string { return exc.reason.Error() }
|
2017-01-28 22:39:52 +08:00
|
|
|
|
2020-03-31 07:27:08 +08:00
|
|
|
// Show shows the exception.
|
2021-01-08 08:56:45 +08:00
|
|
|
func (exc *exception) Show(indent string) string {
|
2017-01-28 22:39:52 +08:00
|
|
|
buf := new(bytes.Buffer)
|
2017-12-19 07:13:41 +08:00
|
|
|
|
|
|
|
var causeDescription string
|
2021-01-08 08:56:45 +08:00
|
|
|
if shower, ok := exc.reason.(diag.Shower); ok {
|
2020-03-31 07:27:08 +08:00
|
|
|
causeDescription = shower.Show(indent)
|
2021-01-08 08:56:45 +08:00
|
|
|
} else if exc.reason == nil {
|
2020-07-02 04:48:03 +08:00
|
|
|
causeDescription = "ok"
|
2017-01-29 10:29:03 +08:00
|
|
|
} else {
|
2021-01-08 08:56:45 +08:00
|
|
|
causeDescription = "\033[31;1m" + exc.reason.Error() + "\033[m"
|
2017-01-29 10:29:03 +08:00
|
|
|
}
|
2020-07-05 02:07:56 +08:00
|
|
|
fmt.Fprintf(buf, "Exception: %s", causeDescription)
|
2017-01-28 22:39:52 +08:00
|
|
|
|
2021-01-08 08:56:45 +08:00
|
|
|
if exc.stackTrace != nil {
|
2020-07-05 02:07:56 +08:00
|
|
|
buf.WriteString("\n")
|
2021-01-08 08:56:45 +08:00
|
|
|
if exc.stackTrace.Next == nil {
|
|
|
|
buf.WriteString(exc.stackTrace.Head.ShowCompact(indent))
|
2020-07-05 02:07:56 +08:00
|
|
|
} else {
|
|
|
|
buf.WriteString(indent + "Traceback:")
|
2021-01-08 08:56:45 +08:00
|
|
|
for tb := exc.stackTrace; tb != nil; tb = tb.Next {
|
2020-07-05 02:07:56 +08:00
|
|
|
buf.WriteString("\n" + indent + " ")
|
2020-09-03 12:54:32 +08:00
|
|
|
buf.WriteString(tb.Head.Show(indent + " "))
|
2020-07-05 02:07:56 +08:00
|
|
|
}
|
2017-12-19 07:13:41 +08:00
|
|
|
}
|
2017-01-28 22:39:52 +08:00
|
|
|
}
|
|
|
|
|
2021-01-08 08:56:45 +08:00
|
|
|
if pipeExcs, ok := exc.reason.(PipelineError); ok {
|
2017-01-30 02:35:18 +08:00
|
|
|
buf.WriteString("\n" + indent + "Caused by:")
|
|
|
|
for _, e := range pipeExcs.Errors {
|
2017-06-20 04:57:51 +08:00
|
|
|
if e == OK {
|
|
|
|
continue
|
|
|
|
}
|
2020-03-31 07:27:08 +08:00
|
|
|
buf.WriteString("\n" + indent + " " + e.Show(indent+" "))
|
2017-01-30 02:35:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-28 22:39:52 +08:00
|
|
|
return buf.String()
|
|
|
|
}
|
2017-01-29 10:29:03 +08:00
|
|
|
|
2019-04-19 05:15:34 +08:00
|
|
|
// Kind returns "exception".
|
2021-01-08 08:56:45 +08:00
|
|
|
func (exc *exception) Kind() string {
|
2017-01-29 10:29:03 +08:00
|
|
|
return "exception"
|
|
|
|
}
|
|
|
|
|
2019-04-19 05:15:34 +08:00
|
|
|
// Repr returns a representation of the exception. It is lossy in that it does
|
|
|
|
// not preserve the stacktrace.
|
2021-01-08 08:56:45 +08:00
|
|
|
func (exc *exception) Repr(indent int) string {
|
|
|
|
if exc.reason == nil {
|
2017-01-29 10:29:03 +08:00
|
|
|
return "$ok"
|
|
|
|
}
|
2021-01-08 08:56:45 +08:00
|
|
|
return "[&reason=" + vals.Repr(exc.reason, indent+1) + "]"
|
2017-01-29 10:29:03 +08:00
|
|
|
}
|
|
|
|
|
2019-04-19 05:15:34 +08:00
|
|
|
// Equal compares by address.
|
2021-01-08 08:56:45 +08:00
|
|
|
func (exc *exception) Equal(rhs interface{}) bool {
|
2017-07-14 08:15:14 +08:00
|
|
|
return exc == rhs
|
|
|
|
}
|
|
|
|
|
2019-04-19 05:15:34 +08:00
|
|
|
// Hash returns the hash of the address.
|
2021-01-08 08:56:45 +08:00
|
|
|
func (exc *exception) Hash() uint32 {
|
2017-08-31 02:52:27 +08:00
|
|
|
return hash.Pointer(unsafe.Pointer(exc))
|
|
|
|
}
|
|
|
|
|
2019-04-19 05:15:34 +08:00
|
|
|
// Bool returns whether this exception has a nil cause; that is, it is $ok.
|
2021-01-08 08:56:45 +08:00
|
|
|
func (exc *exception) Bool() bool {
|
|
|
|
return exc.reason == nil
|
2017-01-29 10:29:03 +08:00
|
|
|
}
|
|
|
|
|
2021-01-08 08:56:45 +08:00
|
|
|
func (exc *exception) Fields() vals.StructMap { return excFields{exc} }
|
2018-03-08 21:15:18 +08:00
|
|
|
|
2021-01-08 08:56:45 +08:00
|
|
|
type excFields struct{ e *exception }
|
2020-06-30 04:26:10 +08:00
|
|
|
|
|
|
|
func (excFields) IsStructMap() {}
|
2021-01-08 08:56:45 +08:00
|
|
|
func (f excFields) Reason() error { return f.e.reason }
|
2018-03-08 21:15:18 +08:00
|
|
|
|
2017-01-29 10:29:03 +08:00
|
|
|
// PipelineError represents the errors of pipelines, in which multiple commands
|
|
|
|
// may error.
|
|
|
|
type PipelineError struct {
|
2021-01-08 08:56:45 +08:00
|
|
|
Errors []Exception
|
2017-01-29 10:29:03 +08:00
|
|
|
}
|
|
|
|
|
2019-04-19 05:15:34 +08:00
|
|
|
// Error returns a plain text representation of the pipeline error.
|
2017-01-29 10:29:03 +08:00
|
|
|
func (pe PipelineError) Error() string {
|
|
|
|
b := new(bytes.Buffer)
|
|
|
|
b.WriteString("(")
|
|
|
|
for i, e := range pe.Errors {
|
|
|
|
if i > 0 {
|
|
|
|
b.WriteString(" | ")
|
|
|
|
}
|
2021-01-08 08:56:45 +08:00
|
|
|
if e == nil || e.Reason() == nil {
|
2017-01-29 10:29:03 +08:00
|
|
|
b.WriteString("<nil>")
|
|
|
|
} else {
|
|
|
|
b.WriteString(e.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b.WriteString(")")
|
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
|
2020-09-05 05:10:47 +08:00
|
|
|
// MakePipelineError builds an error from the execution results of multiple
|
|
|
|
// commands in a pipeline.
|
2020-04-10 08:25:55 +08:00
|
|
|
//
|
2020-09-03 13:48:39 +08:00
|
|
|
// If all elements are either nil or OK, it returns nil. If there is exactly
|
|
|
|
// non-nil non-OK Exception, it returns it. Otherwise, it return a PipelineError
|
|
|
|
// built from the slice, with nil items turned into OK's for easier access from
|
|
|
|
// Elvish code.
|
2021-01-08 08:56:45 +08:00
|
|
|
func MakePipelineError(excs []Exception) error {
|
|
|
|
newexcs := make([]Exception, len(excs))
|
2017-01-30 01:56:13 +08:00
|
|
|
notOK, lastNotOK := 0, 0
|
|
|
|
for i, e := range excs {
|
|
|
|
if e == nil {
|
|
|
|
newexcs[i] = OK
|
|
|
|
} else {
|
|
|
|
newexcs[i] = e
|
2021-01-08 08:56:45 +08:00
|
|
|
if e.Reason() != nil {
|
2017-01-30 01:56:13 +08:00
|
|
|
notOK++
|
|
|
|
lastNotOK = i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
switch notOK {
|
|
|
|
case 0:
|
|
|
|
return nil
|
|
|
|
case 1:
|
|
|
|
return newexcs[lastNotOK]
|
|
|
|
default:
|
|
|
|
return PipelineError{newexcs}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-01 05:26:01 +08:00
|
|
|
func (pe PipelineError) Fields() vals.StructMap { return peFields{pe} }
|
|
|
|
|
|
|
|
type peFields struct{ pe PipelineError }
|
|
|
|
|
|
|
|
func (peFields) IsStructMap() {}
|
|
|
|
|
|
|
|
func (f peFields) Type() string { return "pipeline" }
|
|
|
|
|
|
|
|
func (f peFields) Exceptions() vals.List {
|
|
|
|
li := vals.EmptyList
|
|
|
|
for _, exc := range f.pe.Errors {
|
|
|
|
li = li.Cons(exc)
|
|
|
|
}
|
|
|
|
return li
|
|
|
|
}
|
|
|
|
|
2017-01-29 10:29:03 +08:00
|
|
|
// Flow is a special type of error used for control flows.
|
|
|
|
type Flow uint
|
|
|
|
|
|
|
|
// Control flows.
|
|
|
|
const (
|
|
|
|
Return Flow = iota
|
|
|
|
Break
|
|
|
|
Continue
|
|
|
|
)
|
|
|
|
|
|
|
|
var flowNames = [...]string{
|
|
|
|
"return", "break", "continue",
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f Flow) Error() string {
|
|
|
|
if f >= Flow(len(flowNames)) {
|
2020-03-31 07:36:55 +08:00
|
|
|
return fmt.Sprintf("!(BAD FLOW: %d)", f)
|
2017-01-29 10:29:03 +08:00
|
|
|
}
|
|
|
|
return flowNames[f]
|
|
|
|
}
|
|
|
|
|
2020-03-31 07:27:08 +08:00
|
|
|
// Show shows the flow "error".
|
|
|
|
func (f Flow) Show(string) string {
|
2017-01-29 10:29:03 +08:00
|
|
|
return "\033[33;1m" + f.Error() + "\033[m"
|
|
|
|
}
|
|
|
|
|
2020-07-01 05:26:01 +08:00
|
|
|
func (f Flow) Fields() vals.StructMap { return flowFields{f} }
|
|
|
|
|
|
|
|
type flowFields struct{ f Flow }
|
|
|
|
|
|
|
|
func (flowFields) IsStructMap() {}
|
|
|
|
|
|
|
|
func (f flowFields) Type() string { return "flow" }
|
|
|
|
func (f flowFields) Name() string { return f.f.Error() }
|
|
|
|
|
2020-03-31 06:38:40 +08:00
|
|
|
// ExternalCmdExit contains the exit status of external commands.
|
2017-01-29 10:29:03 +08:00
|
|
|
type ExternalCmdExit struct {
|
|
|
|
syscall.WaitStatus
|
|
|
|
CmdName string
|
|
|
|
Pid int
|
|
|
|
}
|
|
|
|
|
2019-04-19 05:15:34 +08:00
|
|
|
// NewExternalCmdExit constructs an error for representing a non-zero exit from
|
|
|
|
// an external command.
|
2017-01-29 10:29:03 +08:00
|
|
|
func NewExternalCmdExit(name string, ws syscall.WaitStatus, pid int) error {
|
|
|
|
if ws.Exited() && ws.ExitStatus() == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return ExternalCmdExit{ws, name, pid}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (exit ExternalCmdExit) Error() string {
|
|
|
|
ws := exit.WaitStatus
|
|
|
|
quotedName := parse.Quote(exit.CmdName)
|
|
|
|
switch {
|
|
|
|
case ws.Exited():
|
|
|
|
return quotedName + " exited with " + strconv.Itoa(ws.ExitStatus())
|
|
|
|
case ws.Signaled():
|
2017-12-19 07:13:41 +08:00
|
|
|
causeDescription := quotedName + " killed by signal " + ws.Signal().String()
|
2017-01-29 10:29:03 +08:00
|
|
|
if ws.CoreDump() {
|
2017-12-19 07:13:41 +08:00
|
|
|
causeDescription += " (core dumped)"
|
2017-01-29 10:29:03 +08:00
|
|
|
}
|
2017-12-19 07:13:41 +08:00
|
|
|
return causeDescription
|
2017-01-29 10:29:03 +08:00
|
|
|
case ws.Stopped():
|
2017-12-19 07:13:41 +08:00
|
|
|
causeDescription := quotedName + " stopped by signal " + fmt.Sprintf("%s (pid=%d)", ws.StopSignal(), exit.Pid)
|
2017-01-29 10:29:03 +08:00
|
|
|
trap := ws.TrapCause()
|
|
|
|
if trap != -1 {
|
2017-12-19 07:13:41 +08:00
|
|
|
causeDescription += fmt.Sprintf(" (trapped %v)", trap)
|
2017-01-29 10:29:03 +08:00
|
|
|
}
|
2017-12-19 07:13:41 +08:00
|
|
|
return causeDescription
|
2017-01-29 10:29:03 +08:00
|
|
|
default:
|
|
|
|
return fmt.Sprint(quotedName, " has unknown WaitStatus ", ws)
|
|
|
|
}
|
|
|
|
}
|
2020-07-01 05:26:01 +08:00
|
|
|
|
|
|
|
func (exit ExternalCmdExit) Fields() vals.StructMap {
|
|
|
|
ws := exit.WaitStatus
|
|
|
|
f := exitFieldsCommon{exit}
|
|
|
|
switch {
|
|
|
|
case ws.Exited():
|
|
|
|
return exitFieldsExited{f}
|
|
|
|
case ws.Signaled():
|
|
|
|
return exitFieldsSignaled{f}
|
|
|
|
case ws.Stopped():
|
|
|
|
return exitFieldsStopped{f}
|
|
|
|
default:
|
|
|
|
return exitFieldsUnknown{f}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type exitFieldsCommon struct{ e ExternalCmdExit }
|
|
|
|
|
|
|
|
func (exitFieldsCommon) IsStructMap() {}
|
|
|
|
func (f exitFieldsCommon) CmdName() string { return f.e.CmdName }
|
|
|
|
func (f exitFieldsCommon) Pid() string { return strconv.Itoa(f.e.Pid) }
|
|
|
|
|
|
|
|
type exitFieldsExited struct{ exitFieldsCommon }
|
|
|
|
|
|
|
|
func (exitFieldsExited) Type() string { return "external-cmd/exited" }
|
|
|
|
func (f exitFieldsExited) ExitStatus() string { return strconv.Itoa(f.e.ExitStatus()) }
|
|
|
|
|
|
|
|
type exitFieldsSignaled struct{ exitFieldsCommon }
|
|
|
|
|
|
|
|
func (f exitFieldsSignaled) Type() string { return "external-cmd/signaled" }
|
|
|
|
func (f exitFieldsSignaled) SignalName() string { return f.e.Signal().String() }
|
|
|
|
func (f exitFieldsSignaled) SignalNumber() string { return strconv.Itoa(int(f.e.Signal())) }
|
|
|
|
func (f exitFieldsSignaled) CoreDumped() bool { return f.e.CoreDump() }
|
|
|
|
|
|
|
|
type exitFieldsStopped struct{ exitFieldsCommon }
|
|
|
|
|
|
|
|
func (f exitFieldsStopped) Type() string { return "external-cmd/stopped" }
|
|
|
|
func (f exitFieldsStopped) SignalName() string { return f.e.StopSignal().String() }
|
|
|
|
func (f exitFieldsStopped) SignalNumber() string { return strconv.Itoa(int(f.e.StopSignal())) }
|
|
|
|
func (f exitFieldsStopped) TrapCause() int { return f.e.TrapCause() }
|
|
|
|
|
|
|
|
type exitFieldsUnknown struct{ exitFieldsCommon }
|
|
|
|
|
|
|
|
func (exitFieldsUnknown) Type() string { return "external-cmd/unknown" }
|