Skip to content

Commit

Permalink
gojqextra: Move errors to own file
Browse files Browse the repository at this point in the history
  • Loading branch information
wader committed Sep 27, 2021
1 parent f1fcbe5 commit 9035278
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 66 deletions.
72 changes: 72 additions & 0 deletions internal/gojqextra/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package gojqextra

import "fmt"

// many of these based on errors form gojq
// TODO: refactor to use errors from gojq?
// TODO: preview from gojq?

type FuncTypeError struct {
Name string
Typ string
}

func (err FuncTypeError) Error() string { return err.Name + " cannot be applied to: " + err.Typ }

type ExpectedObjectError struct {
Typ string
}

func (err ExpectedObjectError) Error() string {
return "expected an object but got: " + err.Typ
}

type ExpectedArrayError struct {
Typ string
}

func (err ExpectedArrayError) Error() string {
return "expected an array but got: " + err.Typ
}

type ExpectedObjectWithKeyError struct {
Typ string
Key string
}

func (err ExpectedObjectWithKeyError) Error() string {
return fmt.Sprintf("expected an object with key %q but got: %s", err.Key, err.Typ)
}

type ExpectedArrayWithIndexError struct {
Typ string
Index int
}

func (err ExpectedArrayWithIndexError) Error() string {
return fmt.Sprintf("expected an array with index %d but got: %s", err.Index, err.Typ)
}

type IteratorError struct {
Typ string
}

func (err IteratorError) Error() string {
return "cannot iterate over: " + err.Typ
}

type HasKeyTypeError struct {
L, R string
}

func (err HasKeyTypeError) Error() string {
return "cannot check whether " + err.L + " has a key: " + err.R
}

type ArrayIndexTooLargeError struct {
V interface{}
}

func (err *ArrayIndexTooLargeError) Error() string {
return fmt.Sprintf("array index too large: %v", err.V)
}
66 changes: 0 additions & 66 deletions internal/gojqextra/gojqextra.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,74 +10,8 @@ import (
"github.com/wader/gojq"
)

// TODO: refactor to use errors from gojq?
// TODO: preview errors

type FuncTypeError struct {
Name string
Typ string
}

func (err FuncTypeError) Error() string { return err.Name + " cannot be applied to: " + err.Typ }

type ExpectedObjectError struct {
Typ string
}

func (err ExpectedObjectError) Error() string {
return "expected an object but got: " + err.Typ
}

type ExpectedArrayError struct {
Typ string
}

func (err ExpectedArrayError) Error() string {
return "expected an array but got: " + err.Typ
}

type ExpectedObjectWithKeyError struct {
Typ string
Key string
}

func (err ExpectedObjectWithKeyError) Error() string {
return fmt.Sprintf("expected an object with key %q but got: %s", err.Key, err.Typ)
}

type ExpectedArrayWithIndexError struct {
Typ string
Index int
}

func (err ExpectedArrayWithIndexError) Error() string {
return fmt.Sprintf("expected an array with index %d but got: %s", err.Index, err.Typ)
}

type IteratorError struct {
Typ string
}

func (err IteratorError) Error() string {
return "cannot iterate over: " + err.Typ
}

type HasKeyTypeError struct {
L, R string
}

func (err HasKeyTypeError) Error() string {
return "cannot check whether " + err.L + " has a key: " + err.R
}

type ArrayIndexTooLargeError struct {
V interface{}
}

func (err *ArrayIndexTooLargeError) Error() string {
return fmt.Sprintf("array index too large: %v", err.V)
}

func expectedArrayOrObject(key interface{}, typ string) error {
switch v := key.(type) {
case string:
Expand Down

0 comments on commit 9035278

Please sign in to comment.