Skip to content

Commit

Permalink
feat: Optimize error message (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
vircoys authored Dec 22, 2022
1 parent eea045a commit 0df00ee
Show file tree
Hide file tree
Showing 51 changed files with 770 additions and 426 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pkg/parser/y.output
rl.sh
dist/
test.output
tmp/

# Local Netlify folder
.netlify
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ test-cov:
tools:
mkdir -p ./dist
rm -r ./dist/
go build -o ./dist/ cmd/ppl/ppl.go
go build -o ./dist/platypus ./cmd/platypus/

.PHONY: generate
generate:
Expand Down
21 changes: 14 additions & 7 deletions internal/cmd/platypus/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ import (
"github.com/GuanceCloud/platypus/pkg/inimpl/guancecloud/input"
"github.com/influxdata/influxdb1-client/models"
influxdb "github.com/influxdata/influxdb1-client/v2"
"go.uber.org/zap/zapcore"
)

var l = logger.NewStdoutLogger("cli", "debug")
var l = logger.NewStdoutLogger("cli", zapcore.DebugLevel)

func Run(ctx context.Context, options *Options) error {
if options.Script == "" {
Expand All @@ -27,14 +28,20 @@ func Run(ctx context.Context, options *Options) error {

script, err := loadScript(ctx, options)
if err != nil {
return err
l.Error(err)
return nil
}

// check script only
if options.Input == "" {
return nil
}

return runScript(ctx, options, script)
if err := runScript(ctx, options, script); err != nil {
l.Error(err)
}

return nil
}

func loadScript(ctx context.Context, options *Options) (*plruntime.Script, error) {
Expand All @@ -59,7 +66,7 @@ func loadScript(ctx context.Context, options *Options) (*plruntime.Script, error
scripts, errs := engine.ParseScript(scriptsContent, funcs.FuncsMap, funcs.FuncsCheckMap)
if len(errs) > 0 {
if err, ok := errs[options.Script]; ok {
return nil, fmt.Errorf("parse script error: %w", err)
return nil, err
}
}

Expand Down Expand Up @@ -118,7 +125,7 @@ func runScript(ctx context.Context, options *Options, script *plruntime.Script)

errR := engine.RunScriptWithRMapIn(script, pt, nil)
if errR != nil {
return fmt.Errorf("run script error: %w", errR.ChainError())
return fmt.Errorf("run script error: %w", errR)
}

if dropped {
Expand All @@ -139,13 +146,13 @@ func runScript(ctx context.Context, options *Options, script *plruntime.Script)
}); err != nil {
return fmt.Errorf("encode json output error: %w", err)
}
l.Debugf("Platypus Output Data:\n%s", buf.String())
l.Infof("Platypus Output Data:\n%s", buf.String())
case OutTypeLineProtocol:
pt, err := influxdb.NewPoint(measurement, tags, fields, tn)
if err != nil {
return fmt.Errorf("encode point output error: %w", err)
}
l.Debugf("Platypus Output Data:\n%s", pt.String())
l.Infof("Platypus Output Data:\n%s", pt.String())
}
return nil
}
8 changes: 5 additions & 3 deletions internal/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ type Logger interface {
Fatalf(format string, args ...interface{})
}

func NewStdoutLogger(name, level string) Logger {
func NewStdoutLogger(name string, level zapcore.Level) Logger {
cfg := &zap.Config{
Level: zap.NewAtomicLevelAt(zapcore.DebugLevel),
Encoding: `json`,
Level: zap.NewAtomicLevelAt(level),
Encoding: `console`,
EncoderConfig: zapcore.EncoderConfig{
NameKey: NameKeyMod,
MessageKey: NameKeyMsg,
Expand All @@ -47,6 +47,8 @@ func NewStdoutLogger(name, level string) Logger {
},
}

cfg.OutputPaths = append(cfg.OutputPaths, "stdout")

l, err := cfg.Build()
if err != nil {
panic(err)
Expand Down
20 changes: 10 additions & 10 deletions pkg/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type NodeType uint

const (
// expr.
TypeInvaild NodeType = iota
TypeInvalid NodeType = iota

TypeIdentifier
TypeStringLiteral
Expand Down Expand Up @@ -52,8 +52,8 @@ const (

func (t NodeType) String() string {
switch t {
case TypeInvaild:
return "Invaild"
case TypeInvalid:
return "Invalid"
case TypeIdentifier:
return "Identifier"
case TypeStringLiteral:
Expand Down Expand Up @@ -358,17 +358,17 @@ func WrapeBlockStmt(node *BlockStmt) *Node {
}
}

func (node *Node) StartPos() token.Pos {
func (node *Node) StartPos() token.LnColPos {
return NodeStartPos(node)
}

func NodeStartPos(node *Node) token.Pos {
func NodeStartPos(node *Node) token.LnColPos {
if node == nil {
return -1
return token.InvalidLnColPos
}
switch node.NodeType {
case TypeInvaild:
return -1
case TypeInvalid:
return token.InvalidLnColPos
case TypeIdentifier:
return node.Identifier.Start
case TypeStringLiteral:
Expand Down Expand Up @@ -413,7 +413,7 @@ func NodeStartPos(node *Node) token.Pos {
if len(node.IfelseStmt.IfList) > 0 {
return node.IfelseStmt.IfList[0].Start
} else {
return -1
return token.InvalidLnColPos
}

case TypeForStmt:
Expand All @@ -425,5 +425,5 @@ func NodeStartPos(node *Node) token.Pos {
case TypeBreakStmt:
return node.BreakStmt.Start
}
return -1
return token.InvalidLnColPos
}
48 changes: 25 additions & 23 deletions pkg/ast/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const (

type Identifier struct {
Name string
Start token.Pos
Start token.LnColPos
}

func (e *Identifier) IsExpr() bool {
Expand All @@ -54,7 +54,7 @@ func (e *Identifier) String() string {

type StringLiteral struct {
Val string
Start token.Pos
Start token.LnColPos
}

func (e *StringLiteral) IsExpr() bool {
Expand All @@ -69,13 +69,13 @@ func (e *StringLiteral) String() string {
// IsInt bool
// Float float64
// Int int64
// Start token.Pos
// End token.Pos
// Start token.FilePos
// End token.FilePos
// }

type IntegerLiteral struct {
Val int64
Start token.Pos
Start token.LnColPos
}

func (e *IntegerLiteral) String() string {
Expand All @@ -88,7 +88,7 @@ func (e *IntegerLiteral) IsExpr() bool {

type FloatLiteral struct {
Val float64
Start token.Pos
Start token.LnColPos
}

func (e *FloatLiteral) String() string {
Expand All @@ -101,15 +101,15 @@ func (e *FloatLiteral) IsExpr() bool {

type BoolLiteral struct {
Val bool
Start token.Pos
Start token.LnColPos
}

func (e *BoolLiteral) String() string {
return fmt.Sprintf("%v", e.Val)
}

type NilLiteral struct {
Start token.Pos
Start token.LnColPos
}

func (e *NilLiteral) IsExpr() bool {
Expand All @@ -122,8 +122,8 @@ func (e *NilLiteral) String() string {

type MapInitExpr struct {
KeyValeList [][2]*Node // key,value list
LBrace token.Pos
RBrace token.Pos
LBrace token.LnColPos
RBrace token.LnColPos
}

func (e *MapInitExpr) IsExpr() bool {
Expand All @@ -144,8 +144,8 @@ func (e *MapInitExpr) String() string {

type ListInitExpr struct {
List []*Node
LBracket token.Pos
RBracket token.Pos
LBracket token.LnColPos
RBracket token.LnColPos
}

func (e *ListInitExpr) IsExpr() bool {
Expand All @@ -163,7 +163,7 @@ func (e *ListInitExpr) String() string {
type ConditionalExpr struct {
Op Op
LHS, RHS *Node
OpPos token.Pos
OpPos token.LnColPos
}

func (e *ConditionalExpr) IsExpr() bool {
Expand All @@ -177,7 +177,7 @@ func (e *ConditionalExpr) String() string {
type ArithmeticExpr struct {
Op Op
LHS, RHS *Node
OpPos token.Pos
OpPos token.LnColPos
}

func (e *ArithmeticExpr) IsExpr() bool {
Expand All @@ -191,7 +191,7 @@ func (e *ArithmeticExpr) String() string {
type AttrExpr struct {
Obj *Node
Attr *Node
Start token.Pos
Start token.LnColPos
}

func (e *AttrExpr) IsExpr() bool {
Expand All @@ -212,8 +212,8 @@ func (e *AttrExpr) String() string {
type IndexExpr struct {
Obj *Identifier
Index []*Node // int float string bool
LBracket []token.Pos
RBracket []token.Pos
LBracket []token.LnColPos
RBracket []token.LnColPos
}

func (e *IndexExpr) IsExpr() bool {
Expand All @@ -234,8 +234,8 @@ func (e *IndexExpr) String() string {

type ParenExpr struct {
Param *Node
LParen token.Pos
RParen token.Pos
LParen token.LnColPos
RParen token.LnColPos
}

func (e *ParenExpr) IsExpr() bool {
Expand All @@ -251,15 +251,17 @@ type CallExpr struct {
// Name *ast.Node

// temporary record function name location
NamePos token.Pos // as 'Start' (token.Pos)
NamePos token.LnColPos // as 'Start' (token.FilePos)

LParen token.Pos
RParen token.Pos
LParen token.LnColPos
RParen token.LnColPos

Name string

Param []*Node

PrivateData interface{}

// ParamIndex []int

Grok *grok.GrokRegexp
Expand All @@ -280,7 +282,7 @@ func (e *CallExpr) String() string {

type AssignmentExpr struct {
LHS, RHS *Node
OpPos token.Pos
OpPos token.LnColPos
}

func (e *AssignmentExpr) IsExpr() bool {
Expand Down
18 changes: 9 additions & 9 deletions pkg/ast/stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type IfelseStmt struct {
IfList IfList
Else *BlockStmt

ElsePos token.Pos
ElsePos token.LnColPos
}

func (e *IfelseStmt) IsExpr() bool {
Expand Down Expand Up @@ -48,7 +48,7 @@ type IfStmtElem struct {
Condition *Node
Block *BlockStmt

Start token.Pos
Start token.LnColPos
}

func (e *IfStmtElem) String() string {
Expand All @@ -57,15 +57,15 @@ func (e *IfStmtElem) String() string {
}

type BreakStmt struct {
Start token.Pos
Start token.LnColPos
}

func (e *BreakStmt) String() string {
return "break"
}

type ContinueStmt struct {
Start token.Pos
Start token.LnColPos
}

func (e *ContinueStmt) String() string {
Expand All @@ -77,8 +77,8 @@ type ForInStmt struct {
Iter *Node
Body *BlockStmt

ForPos token.Pos
InPos token.Pos
ForPos token.LnColPos
InPos token.LnColPos
}

func (e *ForInStmt) String() string {
Expand All @@ -98,16 +98,16 @@ type ForStmt struct {
// step2: -> step3
Body *BlockStmt

ForPos token.Pos
ForPos token.LnColPos
}

func (e *ForStmt) String() string {
return "for stmt"
}

type BlockStmt struct {
LBracePos token.Pos
RBracePos token.Pos
LBracePos token.LnColPos
RBracePos token.LnColPos
Stmts Stmts
}

Expand Down
Loading

0 comments on commit 0df00ee

Please sign in to comment.