Skip to content

Commit

Permalink
Return only abstract errors (#30)
Browse files Browse the repository at this point in the history
We returned concrete errors (such as `jsonization.DeserializationError`)
instead of abstract `error`'s. This is a common footgun in Go, see [this
page in Golang's documentation].

Instead, in this patch, we always return the abstract `error`'s.

Fixes #28.

[this page in Golang's documentation]: https://go.dev/doc/faq#nil_error
  • Loading branch information
mristin authored May 16, 2024
1 parent 6620ab1 commit c493f8f
Show file tree
Hide file tree
Showing 10 changed files with 5,580 additions and 4,243 deletions.
16 changes: 7 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -543,15 +543,14 @@ func main() {

// Serialize to jsonable
var jsonable map[string]interface{}
var seriaErr *aasjsonization.SerializationError
jsonable, seriaErr = aasjsonization.ToJsonable(environment)
if seriaErr != nil {
panic(seriaErr.Error())
var err error
jsonable, err = aasjsonization.ToJsonable(environment)
if err != nil {
panic(err.Error())
}

// Serialize jsonable to string
var bb []byte
var err error
bb, err = json.MarshalIndent(jsonable, "", " ")
if err != nil {
panic(err.Error())
Expand Down Expand Up @@ -638,12 +637,11 @@ func main() {
}

var environment aastypes.IEnvironment
var deseriaErr *aasjsonization.DeserializationError
environment, deseriaErr = aasjsonization.EnvironmentFromJsonable(
environment, err = aasjsonization.EnvironmentFromJsonable(
jsonable,
)
if deseriaErr != nil {
panic(deseriaErr.Error())
if err != nil {
panic(err.Error())
}

environment.Descend(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ def main() -> int:
{I}jsonable, err := aasjsonization.ToJsonable(instance)
{I}if err != nil {{
{II}t.Fatalf(
{III}"Failed to serialize the minimal {model_type}: %s",
{III}err.Error(),
{III}"Failed to serialize the minimal {model_type}: %v",
{III}err,
{II})
{II}return
{I}}}
Expand Down Expand Up @@ -138,12 +138,18 @@ def main() -> int:
func {test_name}(t *testing.T) {{
{I}jsonable := any("this is not an object")
{I}_, deseriaErr := aasjsonization.{deserialization_function}(
{I}_, err := aasjsonization.{deserialization_function}(
{II}jsonable,
{I})
{I}if deseriaErr == nil {{
{II}t.Fatal("Expected a deserialization error, but got none.")
{I}if err == nil {{
{II}t.Fatal("Expected an error, but got none.")
{II}return
{I}}}
{I}deseriaErr, ok := err.(*aasjsonization.DeserializationError)
{I}if !ok {{
{II}t.Fatalf("Expected a de-serialization error, but got: %v", err)
{II}return
{I}}}
Expand Down
10 changes: 8 additions & 2 deletions _dev_scripts/test_codegen/generate_jsonization_of_enums_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,21 @@ def _generate_deserialization_fail_for_enum(enum: intermediate.Enumeration) -> S
func {test_name}(t *testing.T) {{
{I}jsonable := any("THIS-CANNOT-POSSIBLY-BE-VALID")
{I}_, deseriaErr := aasjsonization.{deserialization_function}(
{I}_, err := aasjsonization.{deserialization_function}(
{II}jsonable,
{I})
{I}if deseriaErr == nil {{
{I}if err == nil {{
{II}t.Fatal("Expected a deserialization error, but got none.")
{II}return
{I}}}
{I}deseriaErr, ok := err.(*aasjsonization.DeserializationError)
{I}if !ok {{
{II}t.Fatalf("Expected a de-serialization error, but got: %v", err)
{II}return
{I}}}
{I}pathString := deseriaErr.PathString()
{I}if len(pathString) != 0 {{
{II}t.Fatalf(
Expand Down
4 changes: 4 additions & 0 deletions aastesting/common_jsonization.generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,11 +535,15 @@ func MustLoadMinimalSubmodel() (result aastypes.ISubmodel) {
"minimal.json",
)

fmt.Printf("Must read jsonable\n")
jsonable := MustReadJsonable(pth)

container, err := aasjsonization.EnvironmentFromJsonable(
jsonable,
)
fmt.Printf("container: %v\n", container)
fmt.Printf("err: %v\n", err)

if err != nil {
panic(
fmt.Sprintf(
Expand Down
7 changes: 3 additions & 4 deletions getting_started/jsonization_from_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,11 @@ func Example_jsonizationFrom() {
}

var environment aastypes.IEnvironment
var deseriaErr *aasjsonization.DeserializationError
environment, deseriaErr = aasjsonization.EnvironmentFromJsonable(
environment, err = aasjsonization.EnvironmentFromJsonable(
jsonable,
)
if deseriaErr != nil {
panic(deseriaErr.Error())
if err != nil {
panic(err.Error())
}

environment.Descend(
Expand Down
9 changes: 4 additions & 5 deletions getting_started/jsonization_to_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,14 @@ func Example_jsonizationTo() {

// Serialize to jsonable
var jsonable map[string]interface{}
var seriaErr *aasjsonization.SerializationError
jsonable, seriaErr = aasjsonization.ToJsonable(environment)
if seriaErr != nil {
panic(seriaErr.Error())
var err error
jsonable, err = aasjsonization.ToJsonable(environment)
if err != nil {
panic(err.Error())
}

// Serialize jsonable to string
var bb []byte
var err error
bb, err = json.MarshalIndent(jsonable, "", " ")
if err != nil {
panic(err.Error())
Expand Down
Loading

0 comments on commit c493f8f

Please sign in to comment.