Skip to content

Commit 5cc5a53

Browse files
authored
improve error messages (#1148)
1 parent 4ed3f6c commit 5cc5a53

File tree

13 files changed

+147
-78
lines changed

13 files changed

+147
-78
lines changed

internal/README.md

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ type ChartOfAccounts map[string]ChartSegment
446446
```
447447

448448
<a name="ChartOfAccounts.FindAccountSchema"></a>
449-
### func \(\*ChartOfAccounts\) [FindAccountSchema](<https://github.com/formancehq/ledger/blob/main/internal/chart.go#L252>)
449+
### func \(\*ChartOfAccounts\) [FindAccountSchema](<https://github.com/formancehq/ledger/blob/main/internal/chart.go#L264>)
450450

451451
```go
452452
func (c *ChartOfAccounts) FindAccountSchema(account string) (*ChartAccount, error)
@@ -473,7 +473,7 @@ func (s *ChartOfAccounts) UnmarshalJSON(data []byte) error
473473

474474

475475
<a name="ChartOfAccounts.ValidatePosting"></a>
476-
### func \(\*ChartOfAccounts\) [ValidatePosting](<https://github.com/formancehq/ledger/blob/main/internal/chart.go#L263>)
476+
### func \(\*ChartOfAccounts\) [ValidatePosting](<https://github.com/formancehq/ledger/blob/main/internal/chart.go#L275>)
477477

478478
```go
479479
func (c *ChartOfAccounts) ValidatePosting(posting Posting) error
@@ -673,7 +673,7 @@ func (s DeletedMetadata) ValidateWithSchema(schema Schema) error
673673

674674

675675
<a name="ErrAlreadyStarted"></a>
676-
## type [ErrAlreadyStarted](<https://github.com/formancehq/ledger/blob/main/internal/errors.go#L75>)
676+
## type [ErrAlreadyStarted](<https://github.com/formancehq/ledger/blob/main/internal/errors.go#L76>)
677677

678678

679679

@@ -682,7 +682,7 @@ type ErrAlreadyStarted string
682682
```
683683

684684
<a name="NewErrAlreadyStarted"></a>
685-
### func [NewErrAlreadyStarted](<https://github.com/formancehq/ledger/blob/main/internal/errors.go#L86>)
685+
### func [NewErrAlreadyStarted](<https://github.com/formancehq/ledger/blob/main/internal/errors.go#L87>)
686686

687687
```go
688688
func NewErrAlreadyStarted(id string) ErrAlreadyStarted
@@ -691,7 +691,7 @@ func NewErrAlreadyStarted(id string) ErrAlreadyStarted
691691

692692

693693
<a name="ErrAlreadyStarted.Error"></a>
694-
### func \(ErrAlreadyStarted\) [Error](<https://github.com/formancehq/ledger/blob/main/internal/errors.go#L77>)
694+
### func \(ErrAlreadyStarted\) [Error](<https://github.com/formancehq/ledger/blob/main/internal/errors.go#L78>)
695695

696696
```go
697697
func (e ErrAlreadyStarted) Error() string
@@ -700,7 +700,7 @@ func (e ErrAlreadyStarted) Error() string
700700

701701

702702
<a name="ErrAlreadyStarted.Is"></a>
703-
### func \(ErrAlreadyStarted\) [Is](<https://github.com/formancehq/ledger/blob/main/internal/errors.go#L81>)
703+
### func \(ErrAlreadyStarted\) [Is](<https://github.com/formancehq/ledger/blob/main/internal/errors.go#L82>)
704704

705705
```go
706706
func (e ErrAlreadyStarted) Is(err error) bool
@@ -709,7 +709,7 @@ func (e ErrAlreadyStarted) Is(err error) bool
709709

710710

711711
<a name="ErrInvalidAccount"></a>
712-
## type [ErrInvalidAccount](<https://github.com/formancehq/ledger/blob/main/internal/errors.go#L102-L105>)
712+
## type [ErrInvalidAccount](<https://github.com/formancehq/ledger/blob/main/internal/errors.go#L103-L107>)
713713

714714

715715

@@ -720,7 +720,7 @@ type ErrInvalidAccount struct {
720720
```
721721

722722
<a name="ErrInvalidAccount.Error"></a>
723-
### func \(ErrInvalidAccount\) [Error](<https://github.com/formancehq/ledger/blob/main/internal/errors.go#L107>)
723+
### func \(ErrInvalidAccount\) [Error](<https://github.com/formancehq/ledger/blob/main/internal/errors.go#L109>)
724724

725725
```go
726726
func (e ErrInvalidAccount) Error() string
@@ -729,7 +729,7 @@ func (e ErrInvalidAccount) Error() string
729729

730730

731731
<a name="ErrInvalidAccount.Is"></a>
732-
### func \(ErrInvalidAccount\) [Is](<https://github.com/formancehq/ledger/blob/main/internal/errors.go#L110>)
732+
### func \(ErrInvalidAccount\) [Is](<https://github.com/formancehq/ledger/blob/main/internal/errors.go#L118>)
733733

734734
```go
735735
func (e ErrInvalidAccount) Is(err error) bool
@@ -738,7 +738,7 @@ func (e ErrInvalidAccount) Is(err error) bool
738738

739739

740740
<a name="ErrInvalidBucketName"></a>
741-
## type [ErrInvalidBucketName](<https://github.com/formancehq/ledger/blob/main/internal/errors.go#L25-L28>)
741+
## type [ErrInvalidBucketName](<https://github.com/formancehq/ledger/blob/main/internal/errors.go#L26-L29>)
742742

743743

744744

@@ -749,7 +749,7 @@ type ErrInvalidBucketName struct {
749749
```
750750

751751
<a name="ErrInvalidBucketName.Error"></a>
752-
### func \(ErrInvalidBucketName\) [Error](<https://github.com/formancehq/ledger/blob/main/internal/errors.go#L30>)
752+
### func \(ErrInvalidBucketName\) [Error](<https://github.com/formancehq/ledger/blob/main/internal/errors.go#L31>)
753753

754754
```go
755755
func (e ErrInvalidBucketName) Error() string
@@ -758,7 +758,7 @@ func (e ErrInvalidBucketName) Error() string
758758

759759

760760
<a name="ErrInvalidBucketName.Is"></a>
761-
### func \(ErrInvalidBucketName\) [Is](<https://github.com/formancehq/ledger/blob/main/internal/errors.go#L34>)
761+
### func \(ErrInvalidBucketName\) [Is](<https://github.com/formancehq/ledger/blob/main/internal/errors.go#L35>)
762762

763763
```go
764764
func (e ErrInvalidBucketName) Is(err error) bool
@@ -767,7 +767,7 @@ func (e ErrInvalidBucketName) Is(err error) bool
767767

768768

769769
<a name="ErrInvalidLedgerName"></a>
770-
## type [ErrInvalidLedgerName](<https://github.com/formancehq/ledger/blob/main/internal/errors.go#L7-L10>)
770+
## type [ErrInvalidLedgerName](<https://github.com/formancehq/ledger/blob/main/internal/errors.go#L8-L11>)
771771

772772

773773

@@ -778,7 +778,7 @@ type ErrInvalidLedgerName struct {
778778
```
779779

780780
<a name="ErrInvalidLedgerName.Error"></a>
781-
### func \(ErrInvalidLedgerName\) [Error](<https://github.com/formancehq/ledger/blob/main/internal/errors.go#L12>)
781+
### func \(ErrInvalidLedgerName\) [Error](<https://github.com/formancehq/ledger/blob/main/internal/errors.go#L13>)
782782

783783
```go
784784
func (e ErrInvalidLedgerName) Error() string
@@ -787,7 +787,7 @@ func (e ErrInvalidLedgerName) Error() string
787787

788788

789789
<a name="ErrInvalidLedgerName.Is"></a>
790-
### func \(ErrInvalidLedgerName\) [Is](<https://github.com/formancehq/ledger/blob/main/internal/errors.go#L16>)
790+
### func \(ErrInvalidLedgerName\) [Is](<https://github.com/formancehq/ledger/blob/main/internal/errors.go#L17>)
791791

792792
```go
793793
func (e ErrInvalidLedgerName) Is(err error) bool
@@ -796,7 +796,7 @@ func (e ErrInvalidLedgerName) Is(err error) bool
796796

797797

798798
<a name="ErrInvalidSchema"></a>
799-
## type [ErrInvalidSchema](<https://github.com/formancehq/ledger/blob/main/internal/errors.go#L90-L92>)
799+
## type [ErrInvalidSchema](<https://github.com/formancehq/ledger/blob/main/internal/errors.go#L91-L93>)
800800

801801

802802

@@ -807,7 +807,7 @@ type ErrInvalidSchema struct {
807807
```
808808

809809
<a name="ErrInvalidSchema.Error"></a>
810-
### func \(ErrInvalidSchema\) [Error](<https://github.com/formancehq/ledger/blob/main/internal/errors.go#L94>)
810+
### func \(ErrInvalidSchema\) [Error](<https://github.com/formancehq/ledger/blob/main/internal/errors.go#L95>)
811811

812812
```go
813813
func (e ErrInvalidSchema) Error() string
@@ -816,7 +816,7 @@ func (e ErrInvalidSchema) Error() string
816816

817817

818818
<a name="ErrInvalidSchema.Is"></a>
819-
### func \(ErrInvalidSchema\) [Is](<https://github.com/formancehq/ledger/blob/main/internal/errors.go#L97>)
819+
### func \(ErrInvalidSchema\) [Is](<https://github.com/formancehq/ledger/blob/main/internal/errors.go#L98>)
820820

821821
```go
822822
func (e ErrInvalidSchema) Is(err error) bool
@@ -825,7 +825,7 @@ func (e ErrInvalidSchema) Is(err error) bool
825825

826826

827827
<a name="ErrPipelineAlreadyExists"></a>
828-
## type [ErrPipelineAlreadyExists](<https://github.com/formancehq/ledger/blob/main/internal/errors.go#L45>)
828+
## type [ErrPipelineAlreadyExists](<https://github.com/formancehq/ledger/blob/main/internal/errors.go#L46>)
829829

830830
ErrPipelineAlreadyExists denotes a pipeline already created The store is in charge of returning this error on a failing call on Store.CreatePipeline
831831

@@ -834,7 +834,7 @@ type ErrPipelineAlreadyExists PipelineConfiguration
834834
```
835835

836836
<a name="NewErrPipelineAlreadyExists"></a>
837-
### func [NewErrPipelineAlreadyExists](<https://github.com/formancehq/ledger/blob/main/internal/errors.go#L56>)
837+
### func [NewErrPipelineAlreadyExists](<https://github.com/formancehq/ledger/blob/main/internal/errors.go#L57>)
838838

839839
```go
840840
func NewErrPipelineAlreadyExists(pipelineConfiguration PipelineConfiguration) ErrPipelineAlreadyExists
@@ -843,7 +843,7 @@ func NewErrPipelineAlreadyExists(pipelineConfiguration PipelineConfiguration) Er
843843

844844

845845
<a name="ErrPipelineAlreadyExists.Error"></a>
846-
### func \(ErrPipelineAlreadyExists\) [Error](<https://github.com/formancehq/ledger/blob/main/internal/errors.go#L47>)
846+
### func \(ErrPipelineAlreadyExists\) [Error](<https://github.com/formancehq/ledger/blob/main/internal/errors.go#L48>)
847847

848848
```go
849849
func (e ErrPipelineAlreadyExists) Error() string
@@ -852,7 +852,7 @@ func (e ErrPipelineAlreadyExists) Error() string
852852

853853

854854
<a name="ErrPipelineAlreadyExists.Is"></a>
855-
### func \(ErrPipelineAlreadyExists\) [Is](<https://github.com/formancehq/ledger/blob/main/internal/errors.go#L51>)
855+
### func \(ErrPipelineAlreadyExists\) [Is](<https://github.com/formancehq/ledger/blob/main/internal/errors.go#L52>)
856856

857857
```go
858858
func (e ErrPipelineAlreadyExists) Is(err error) bool
@@ -861,7 +861,7 @@ func (e ErrPipelineAlreadyExists) Is(err error) bool
861861

862862

863863
<a name="ErrPipelineNotFound"></a>
864-
## type [ErrPipelineNotFound](<https://github.com/formancehq/ledger/blob/main/internal/errors.go#L60>)
864+
## type [ErrPipelineNotFound](<https://github.com/formancehq/ledger/blob/main/internal/errors.go#L61>)
865865

866866

867867

@@ -870,7 +870,7 @@ type ErrPipelineNotFound string
870870
```
871871

872872
<a name="NewErrPipelineNotFound"></a>
873-
### func [NewErrPipelineNotFound](<https://github.com/formancehq/ledger/blob/main/internal/errors.go#L71>)
873+
### func [NewErrPipelineNotFound](<https://github.com/formancehq/ledger/blob/main/internal/errors.go#L72>)
874874

875875
```go
876876
func NewErrPipelineNotFound(id string) ErrPipelineNotFound
@@ -879,7 +879,7 @@ func NewErrPipelineNotFound(id string) ErrPipelineNotFound
879879

880880

881881
<a name="ErrPipelineNotFound.Error"></a>
882-
### func \(ErrPipelineNotFound\) [Error](<https://github.com/formancehq/ledger/blob/main/internal/errors.go#L62>)
882+
### func \(ErrPipelineNotFound\) [Error](<https://github.com/formancehq/ledger/blob/main/internal/errors.go#L63>)
883883

884884
```go
885885
func (e ErrPipelineNotFound) Error() string
@@ -888,7 +888,7 @@ func (e ErrPipelineNotFound) Error() string
888888

889889

890890
<a name="ErrPipelineNotFound.Is"></a>
891-
### func \(ErrPipelineNotFound\) [Is](<https://github.com/formancehq/ledger/blob/main/internal/errors.go#L66>)
891+
### func \(ErrPipelineNotFound\) [Is](<https://github.com/formancehq/ledger/blob/main/internal/errors.go#L67>)
892892

893893
```go
894894
func (e ErrPipelineNotFound) Is(err error) bool

internal/api/common/errors.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,16 @@ func HandleCommonWriteErrors(w http.ResponseWriter, r *http.Request, err error)
4848
switch {
4949
case errors.Is(err, ledgercontroller.ErrIdempotencyKeyConflict{}):
5050
api.WriteErrorResponse(w, http.StatusConflict, ErrConflict, err)
51-
case errors.Is(err, ledgercontroller.ErrInvalidIdempotencyInput{}) ||
52-
errors.Is(err, ledgercontroller.ErrSchemaValidationError{}):
51+
case errors.Is(err, ledgercontroller.ErrInvalidIdempotencyInput{}):
5352
api.BadRequest(w, ErrValidation, err)
5453
case errors.Is(err, ledgercontroller.ErrNotFound):
5554
api.NotFound(w, err)
55+
case errors.Is(err, ledgercontroller.ErrSchemaValidationError{}):
56+
api.BadRequest(w, ErrValidation, errors.Unwrap(err))
5657
case errors.Is(err, ledgercontroller.ErrSchemaNotSpecified{}):
57-
api.BadRequest(w, ErrSchemaNotSpecified, err)
58+
api.BadRequest(w, ErrSchemaNotSpecified, errors.Unwrap(err))
5859
case errors.Is(err, ledgercontroller.ErrSchemaNotFound{}):
59-
api.NotFound(w, err)
60+
api.NotFound(w, errors.Unwrap(err))
6061
default:
6162
HandleCommonErrors(w, r, err)
6263
}

internal/api/v2/controllers_schema_insert_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"encoding/json"
66
"errors"
7+
"fmt"
78
"net/http"
89
"net/http/httptest"
910
"testing"
@@ -79,7 +80,7 @@ func TestInsertSchema(t *testing.T) {
7980
expectStatusCode: http.StatusBadRequest,
8081
expectedErrorCode: "VALIDATION",
8182
expectBackendCall: true,
82-
returnErr: ledgercontroller.ErrSchemaValidationError{},
83+
returnErr: fmt.Errorf("unexpected error while forging log: %w", ledgercontroller.ErrSchemaValidationError{}),
8384
},
8485
}
8586

internal/chart.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,11 @@ func findAccountSchema(path []string, fixedSegments map[string]ChartSegment, var
229229
} else if segment.Account != nil {
230230
return segment.Account, nil
231231
} else {
232-
return nil, ErrInvalidAccount{path, nextSegment}
232+
return nil, ErrInvalidAccount{
233+
path: path,
234+
segment: nextSegment,
235+
patternMismatch: false,
236+
}
233237
}
234238
}
235239
if variableSegment != nil {
@@ -243,11 +247,19 @@ func findAccountSchema(path []string, fixedSegments map[string]ChartSegment, var
243247
} else if variableSegment.Account != nil {
244248
return variableSegment.Account, nil
245249
} else {
246-
return nil, ErrInvalidAccount{path, nextSegment}
250+
return nil, ErrInvalidAccount{
251+
path: path,
252+
segment: nextSegment,
253+
patternMismatch: false,
254+
}
247255
}
248256
}
249257
}
250-
return nil, ErrInvalidAccount{path, nextSegment}
258+
return nil, ErrInvalidAccount{
259+
path: path,
260+
segment: nextSegment,
261+
patternMismatch: variableSegment != nil,
262+
}
251263
}
252264
func (c *ChartOfAccounts) FindAccountSchema(account string) (*ChartAccount, error) {
253265
schema, err := findAccountSchema([]string{}, map[string]ChartSegment(*c), nil, strings.Split(account, ":"))

internal/chart_test.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -329,20 +329,25 @@ func TestAccountValidation(t *testing.T) {
329329
},
330330
},
331331
},
332+
{
333+
name: "invalid root segment",
334+
address: "bonk:012",
335+
expectedError: "account starting with `bonk` is not defined in the chart of accounts",
336+
},
332337
{
333338
name: "invalid variable segment",
334-
address: "users:abc:main",
335-
expectedError: "segment `abc` is not allowed by the chart of accounts at `[users]`",
339+
address: "bank:invalid",
340+
expectedError: "segment `invalid` defined by the chart of account at `bank` does not match the pattern",
336341
},
337342
{
338-
name: "non-account variable branch",
339-
address: "users:001",
340-
expectedError: "segment `001` is not allowed by the chart of accounts at `[users]`",
343+
name: "invalid non-root non-variable segment",
344+
address: "users:001:nope",
345+
expectedError: "segment `nope` is not allowed by the chart of accounts at `users:001`",
341346
},
342347
{
343-
name: "non-account fixed branch",
344-
address: "users",
345-
expectedError: "segment `users` is not allowed by the chart of accounts at `[]`",
348+
name: "non-account variable branch",
349+
address: "users:001",
350+
expectedError: "segment `001` is not allowed by the chart of accounts at `users`",
346351
},
347352
} {
348353
if tc.expectedAccount != nil {

internal/controller/ledger/controller_default_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ func testCreateTransaction(t *testing.T, withSchema bool) {
8686
Return(&schema, nil)
8787
} else {
8888
store.EXPECT().
89-
FindSchemas(gomock.Any(), gomock.Any()).
90-
Return(&bunpaginate.Cursor[ledger.Schema]{}, nil)
89+
FindLatestSchemaVersion(gomock.Any()).
90+
Return(nil, nil)
9191
}
9292

9393
store.EXPECT().
@@ -135,8 +135,8 @@ func TestRevertTransaction(t *testing.T) {
135135
Return(store, &bun.Tx{}, nil)
136136

137137
store.EXPECT().
138-
FindSchemas(gomock.Any(), gomock.Any()).
139-
Return(&bunpaginate.Cursor[ledger.Schema]{}, nil)
138+
FindLatestSchemaVersion(gomock.Any()).
139+
Return(nil, nil)
140140

141141
store.EXPECT().
142142
Commit(gomock.Any()).
@@ -195,8 +195,8 @@ func TestSaveTransactionMetadata(t *testing.T) {
195195
Return(store, &bun.Tx{}, nil)
196196

197197
store.EXPECT().
198-
FindSchemas(gomock.Any(), gomock.Any()).
199-
Return(&bunpaginate.Cursor[ledger.Schema]{}, nil)
198+
FindLatestSchemaVersion(gomock.Any()).
199+
Return(nil, nil)
200200

201201
store.EXPECT().
202202
Commit(gomock.Any()).
@@ -245,8 +245,8 @@ func TestDeleteTransactionMetadata(t *testing.T) {
245245
Return(store, &bun.Tx{}, nil)
246246

247247
store.EXPECT().
248-
FindSchemas(gomock.Any(), gomock.Any()).
249-
Return(&bunpaginate.Cursor[ledger.Schema]{}, nil)
248+
FindLatestSchemaVersion(gomock.Any()).
249+
Return(nil, nil)
250250

251251
store.EXPECT().
252252
Commit(gomock.Any()).

0 commit comments

Comments
 (0)