Skip to content

Commit

Permalink
more error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ascandone committed Oct 9, 2024
1 parent c50aed6 commit afa6f4d
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 4 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ require (
github.com/eapache/queue v1.1.0 // indirect
github.com/ericlagergren/decimal v0.0.0-20240411145413-00de7ca16731 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/formancehq/numscript v0.0.9-0.20241007165056-153e7139a0d6 // indirect
github.com/formancehq/numscript v0.0.9-0.20241009144012-1150c14a1417 // indirect
github.com/getsentry/sentry-go v0.28.1 // indirect
github.com/go-chi/chi v4.1.2+incompatible // indirect
github.com/go-chi/render v1.0.3 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ github.com/formancehq/numscript v0.0.9-0.20241007145104-93591532fb96 h1:Y+B10r16
github.com/formancehq/numscript v0.0.9-0.20241007145104-93591532fb96/go.mod h1:btuSv05cYwi9BvLRxVs5zrunU+O1vTgigG1T6UsawcY=
github.com/formancehq/numscript v0.0.9-0.20241007165056-153e7139a0d6 h1:VtiK2H3svTfUe1UlsaajynICc9t+XwXCBbHgEwv416Q=
github.com/formancehq/numscript v0.0.9-0.20241007165056-153e7139a0d6/go.mod h1:btuSv05cYwi9BvLRxVs5zrunU+O1vTgigG1T6UsawcY=
github.com/formancehq/numscript v0.0.9-0.20241009142721-febe5da15eb9 h1:ZvVrISOuEaXqpRdTyaSbHGjzObWtk454EQbdMrDUS/Y=
github.com/formancehq/numscript v0.0.9-0.20241009142721-febe5da15eb9/go.mod h1:btuSv05cYwi9BvLRxVs5zrunU+O1vTgigG1T6UsawcY=
github.com/formancehq/numscript v0.0.9-0.20241009144012-1150c14a1417 h1:LOd5hxnXDIBcehFrpW1OnXk+VSs0yJXeu1iAOO+Hji4=
github.com/formancehq/numscript v0.0.9-0.20241009144012-1150c14a1417/go.mod h1:btuSv05cYwi9BvLRxVs5zrunU+O1vTgigG1T6UsawcY=
github.com/fortytw2/leaktest v1.3.0 h1:u8491cBMTQ8ft8aeV+adlcytMZylmA5nnwwkRZjI8vw=
github.com/fortytw2/leaktest v1.3.0/go.mod h1:jDsjWgpAGjm2CA7WthBh/CdZYEPF31XHquHwclZch5g=
github.com/getsentry/sentry-go v0.28.1 h1:zzaSm/vHmGllRM6Tpx1492r0YDzauArdBfkJRtY6P5k=
Expand Down
5 changes: 5 additions & 0 deletions internal/api/v2/controllers_transactions_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
ledgercontroller "github.com/formancehq/ledger/internal/controller/ledger"

"errors"

"github.com/formancehq/go-libs/api"
"github.com/formancehq/ledger/internal/api/common"
)
Expand Down Expand Up @@ -45,6 +46,10 @@ func createTransaction(w http.ResponseWriter, r *http.Request) {
api.WriteErrorResponse(w, http.StatusConflict, ErrConflict, err)
case errors.Is(err, ledgercontroller.ErrInvalidIdempotencyInput{}):
api.BadRequest(w, ErrValidation, err)
case errors.Is(err, ledgercontroller.ErrParsing{}):
api.BadRequest(w, ErrInterpreterParse, err)
case errors.Is(err, ledgercontroller.ErrRuntime{}):
api.BadRequest(w, ErrInterpreterRuntime, err)
default:
api.InternalServerError(w, r, err)
}
Expand Down
3 changes: 3 additions & 0 deletions internal/api/v2/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ const (
ErrNoPostings = "NO_POSTINGS"
ErrCompilationFailed = "COMPILATION_FAILED"
ErrMetadataOverride = "METADATA_OVERRIDE"

ErrInterpreterParse = "INTERPRETER_PARSE"
ErrInterpreterRuntime = "INTERPRETER_RUNTIME"
)
31 changes: 31 additions & 0 deletions internal/controller/ledger/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

"github.com/formancehq/go-libs/platform/postgres"
"github.com/formancehq/numscript"

"github.com/formancehq/ledger/internal/machine"

Expand Down Expand Up @@ -186,6 +187,36 @@ func newErrCompilationFailed(err error) ErrCompilationFailed {
}
}

type ErrRuntime struct {
Source string
Inner numscript.InterpreterError
}

func (e ErrRuntime) Error() string {
return e.Inner.Error()
}

func (e ErrRuntime) Is(err error) bool {
_, ok := err.(ErrRuntime)
return ok
}

type ErrParsing struct {
Source string
// Precondition: Errors is not empty
Errors []numscript.ParserError
}

func (e ErrParsing) Error() string {
// TODO write a decent description
return "Got parsing errors"
}

func (e ErrParsing) Is(err error) bool {
_, ok := err.(ErrParsing)
return ok
}

// ErrMetadataOverride is used when a metadata is defined at numscript level AND at the input level
type ErrMetadataOverride struct {
key string
Expand Down
5 changes: 4 additions & 1 deletion internal/controller/ledger/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,10 @@ func NewDefaultInterpreterMachineAdapter(parseResult numscript.ParseResult) *Def
func (d *DefaultInterpreterMachineAdapter) Execute(ctx context.Context, tx TX, vars map[string]string) (*MachineResult, error) {
execResult, err := d.parseResult.Run(ctx, vars, newNumscriptRewriteAdapter(tx))
if err != nil {
return nil, err
return nil, ErrRuntime{
Source: d.parseResult.GetSource(),
Inner: err,
}
}

return &MachineResult{
Expand Down
6 changes: 4 additions & 2 deletions internal/controller/ledger/machine_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ var _ MachineFactory = (*DefaultInterpreterMachineFactory)(nil)

func (*DefaultInterpreterMachineFactory) Make(script string) (Machine, error) {
parseResult := numscript.Parse(script)
if len(parseResult.GetParsingErrors()) != 0 {
return nil, nil
errs := parseResult.GetParsingErrors()

if len(errs) != 0 {
return nil, ErrParsing{Source: script, Errors: errs}
}

return NewDefaultInterpreterMachineAdapter(parseResult), nil
Expand Down

0 comments on commit afa6f4d

Please sign in to comment.