Skip to content

Commit

Permalink
Merge pull request #5739 from heshanpadmasiri/fix/error-BBE
Browse files Browse the repository at this point in the history
Add suggestions from Error BBE PR
  • Loading branch information
MaryamZi authored Oct 15, 2024
2 parents 7bf8e03 + 02e2915 commit 6c5444a
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 25 deletions.
1 change: 1 addition & 0 deletions examples/check-expression/check_expression.bal
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import ballerina/io;

// Convert `bytes` to a `string` value and then to an `int` value.
function intFromBytes(byte[] bytes) returns int|error {
string|error res = string:fromBytes(bytes);
// Explicitly check if the result is an error and
Expand Down
2 changes: 2 additions & 0 deletions examples/error-reporting/error_reporting.bal
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public function main() {
{name: "Bob", age: -1},
{name: "Charlie", age: 30}
];
// Note how the `Person` value after the value for which validation fails is
// not processed.
error? err = validatePeople(people);
if err is error {
printError(err);
Expand Down
4 changes: 2 additions & 2 deletions examples/error-reporting/error_reporting.out
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ Validating Alice
Validating Bob
Message: Validation failed for a person
Detail: {"person":{"name":"Bob","age":-1}}
Stack trace: [callableName: validatePeople fileName: error_reporting.bal lineNumber: 20,callableName: main fileName: error_reporting.bal lineNumber: 40]
Stack trace: [callableName: validatePeople fileName: error_reporting.bal lineNumber: 20,callableName: main fileName: error_reporting.bal lineNumber: 42]
Cause:
Message: Age cannot be negative
Detail: {"age":-1}
Stack trace: [callableName: validatePerson fileName: error_reporting.bal lineNumber: 30,callableName: validatePeople fileName: error_reporting.bal lineNumber: 16,callableName: main fileName: error_reporting.bal lineNumber: 40]
Stack trace: [callableName: validatePerson fileName: error_reporting.bal lineNumber: 30,callableName: validatePeople fileName: error_reporting.bal lineNumber: 16,callableName: main fileName: error_reporting.bal lineNumber: 42]
4 changes: 2 additions & 2 deletions examples/error-subtyping/error_subtyping.bal
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ type InvalidI32Detail record {|
// Error with the `InvalidIntDetail` type as the detail type.
type InvalidIntError error<InvalidIntDetail>;

// `error` with `InvalidI32Detail` as the detail type. Thus it is a subtype of `InvalidIntError`.
// Error with `InvalidI32Detail` as the detail type. Thus it is a subtype of `InvalidIntError`.
type InvalidI32Error error<InvalidI32Detail>;

// Distinct error with the `InvalidIntDetail` type as the detail type and a unique type ID.
// Therefore, this is a proper subtype of `InvalidIntError`, but doesn't have a subtype relationship
// with `AnotherDistinctIntError` because they have different type IDs.
type DistinctIntError distinct error<InvalidIntDetail>;

// Another `error` with `InvalidIntDetail` as the detail type and different type ID to `DistinctIntError`
// Another distinct error with `InvalidIntDetail` as the detail type, but a different type ID to `DistinctIntError`
// This is also a proper subtype of `InvalidIntError`, but doesn't have a subtype relationship with `DistinctIntError`
type AnotherDistinctIntError distinct error<InvalidIntDetail>;

Expand Down
4 changes: 2 additions & 2 deletions examples/error-subtyping/error_subtyping.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Error subtyping

If we want to identify if a given `error` type (say `ESub`) is a subtype of another error type (say `ESuper`), first we need to check if `ESuper` is a distinct error type. If it is not then `ESub` is a subtype if and only if the detail type of `ESub` is a subtype of the detail type of `ESuper`.
If we want to identify if a given `error` type (say `ESub`) is a subtype of another error type (say `ESuper`), first we need to check if `ESuper` is a distinct error type. If it is not, then `ESub` is a subtype if and only if the detail type of `ESub` is a subtype of the detail type of `ESuper`.

If more explicit control over error type relations is desired you can use `distinct` error types. Each declaration of a distinct error type has a unique type ID. If `ESuper` is a distinct error type there is the additional requirement that type ID of `ESub` must belong to the type ID set of `ESuper` for it to be a subtype.
If more explicit control over error type relations is desired you can use `distinct` error types. Each declaration of a distinct error type has a unique type ID. If `ESuper` is a distinct error type there is the additional requirement that the type ID set of `ESub` must contain all the type IDs of `ESuper`. In other words, with distinct error types, typing relationships can be made more like nominal typing.

Note that you can create subtypes of distinct error types by intersecting them with other error types.

Expand Down
28 changes: 10 additions & 18 deletions examples/error-type-intersection/error_type_intersection.bal
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,28 @@ type InputError error<InputErrorDetail>;

type NumericError error<NumericErrorDetail>;

type DistinctInputError distinct error<InputErrorDetail>;

type DistinctNumericError distinct error<NumericErrorDetail>;

// `NumericInputError` has detail type, `record {| int value |}`.
type NumericInputError InputError & NumericError;

// `DistinctNumericInputError` has type ids of both `DistinctInputError` and `DistinctNumericError`.
type DistinctNumericInputError DistinctInputError & DistinctNumericError;
type DistinctInputError distinct error<InputErrorDetail>;

function createNumericInputError(int value) returns NumericInputError {
return error("Numeric input error", value = value);
}
type DistinctNumericError distinct error<NumericErrorDetail>;

function createDistinctNumericInputError(int value) returns DistinctNumericInputError {
return error("Distinct numeric input error", value = value);
}
// `DistinctNumericInputError` has type IDs of both `DistinctInputError` and `DistinctNumericError`.
type DistinctNumericInputError DistinctInputError & DistinctNumericError;

public function main() {
NumericInputError e1 = createNumericInputError(5);
// `e1` belong to `InputError` since its detail type is a subtype of `InputErrorDetail`.
NumericInputError e1 = error("Numeric input error", value = 5);
// `e1` belongs to `InputError` since its detail type is a subtype of `InputErrorDetail`.
io:println(e1 is InputError);

// `e1` doesn't belong to `DistinctInputError` since it doesn't have the type id of `DistinctInputError`.
// `e1` doesn't belong to `DistinctInputError` since it doesn't have the type ID of `DistinctInputError`.
io:println(e1 is DistinctInputError);

DistinctNumericInputError e2 = createDistinctNumericInputError(5);
// `e2` belong to `InputError` since it's detail type is a subtype of `InputErrorDetail`.
DistinctNumericInputError e2 = error("Distinct numeric input error", value = 5);
// `e2` belongs to `InputError` since its detail type is a subtype of `InputErrorDetail`.
io:println(e2 is InputError);

// `e2` belong to `DistinctInputError` since it's type id set include the type id of `DistinctInputError`.
// `e2` belongs to `DistinctInputError` since its type ID set includes the type id of `DistinctInputError`.
io:println(e2 is DistinctInputError);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Type intersection for error types

If you intersect two `error` types, the resulting type's detail type is the intersection of the detail types of both types. Furthermore, if any of the types being intersected is a distinct type, then the resultant type's type ID set includes all the type IDs of that type. Thus it is a subtype of both types and this is how you create subtypes of `distinct` error types.
If you intersect two `error` types, the resulting type's detail type is the intersection of the detail types of both types. Furthermore, if any of the types being intersected is a distinct type, then the resultant type's type ID set includes all the type IDs of that type. Thus it is a subtype of both types. This way, you can create an error type that is a subtype of multiple distinct types and also use a more specific detail type.

::: code error_type_intersection.bal :::

Expand Down

0 comments on commit 6c5444a

Please sign in to comment.