Skip to content

Commit

Permalink
change add error definition
Browse files Browse the repository at this point in the history
  • Loading branch information
tatarco committed Dec 13, 2024
1 parent 5dd06df commit 6551289
Show file tree
Hide file tree
Showing 33 changed files with 256 additions and 96 deletions.
36 changes: 18 additions & 18 deletions .speakeasy/gen.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion .speakeasy/gen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ generation:
oAuth2ClientCredentialsEnabled: false
oAuth2PasswordEnabled: false
typescript:
version: 0.0.1-alpha.100
version: 0.0.1-alpha.104
additionalDependencies:
dependencies: {}
devDependencies: {}
Expand Down
12 changes: 6 additions & 6 deletions .speakeasy/workflow.lock
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
speakeasyVersion: 1.454.0
speakeasyVersion: 1.454.2
sources:
json-development:
sourceNamespace: json-development
sourceRevisionDigest: sha256:294fea636b9043a239268abece5047b61e3ecb0feba55c5001158070307aa267
sourceBlobDigest: sha256:24474dba5f53785030e6fbd5bd20447f8904211b67ee2bc2b77f716da10077dc
sourceRevisionDigest: sha256:d6ae711cc32325036df56c1cdcb3b883b2ae0408571690104d13c4f719e04f06
sourceBlobDigest: sha256:b088cb8ed2917c095114cd71609109483bc853175e733c69c86ebc493778889b
tags:
- latest
- "1.0"
targets:
my-first-target:
source: json-development
sourceNamespace: json-development
sourceRevisionDigest: sha256:294fea636b9043a239268abece5047b61e3ecb0feba55c5001158070307aa267
sourceBlobDigest: sha256:24474dba5f53785030e6fbd5bd20447f8904211b67ee2bc2b77f716da10077dc
sourceRevisionDigest: sha256:d6ae711cc32325036df56c1cdcb3b883b2ae0408571690104d13c4f719e04f06
sourceBlobDigest: sha256:b088cb8ed2917c095114cd71609109483bc853175e733c69c86ebc493778889b
codeSamplesNamespace: code-samples-typescript
codeSamplesRevisionDigest: sha256:99fe230c3b134587b641733720e8fc3889fca2ba42164b7c4d4b206716399486
codeSamplesRevisionDigest: sha256:f6e4ad28bb30a2ef69112abafd0be67fe66dbabd3601c996d509624acf744a8c
workflow:
workflowVersion: 1.0.0
speakeasyVersion: latest
Expand Down
32 changes: 17 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -415,25 +415,15 @@ run();
<!-- Start Error Handling [errors] -->
## Error Handling

All SDK methods return a response object or throw an error. By default, an API error will throw a `errors.SDKError`.

If a HTTP request fails, an operation my also throw an error from the `models/errors/httpclienterrors.ts` module:

| HTTP Client Error | Description |
| ---------------------------------------------------- | ---------------------------------------------------- |
| RequestAbortedError | HTTP request was aborted by the client |
| RequestTimeoutError | HTTP request timed out due to an AbortSignal signal |
| ConnectionError | HTTP client was unable to make a request to a server |
| InvalidRequestError | Any input used to create a request is invalid |
| UnexpectedClientError | Unrecognised or unexpected error |

In addition, when custom error responses are specified for an operation, the SDK may throw their associated Error type. You can refer to respective *Errors* tables in SDK docs for more details on possible error types for each operation. For example, the `trigger` method may throw the following errors:
Some methods specify known errors which can be thrown. All the known errors are enumerated in the `models/errors/errors.ts` module. The known errors for a method are documented under the *Errors* tables in SDK docs. For example, the `trigger` method may throw the following errors:

| Error Type | Status Code | Content Type |
| --------------- | ------------- | ---------------- |
| errors.ErrorDto | 400, 404, 409 | application/json |
| errors.SDKError | 4XX, 5XX | \*/\* |

If the method throws an error and it is not captured by the known errors, it will default to throwing a `SDKError`.

```typescript
import { Novu } from "@novu/api";
import { ErrorDto, SDKValidationError } from "@novu/api/models/errors";
Expand Down Expand Up @@ -463,8 +453,9 @@ async function run() {
console.log(result);
} catch (err) {
switch (true) {
// The server response does not match the expected SDK schema
case (err instanceof SDKValidationError): {
// Validation errors can be pretty-printed
// Pretty-print will provide a human-readable multi-line error message
console.error(err.pretty());
// Raw value may also be inspected
console.error(err.rawValue);
Expand All @@ -476,6 +467,7 @@ async function run() {
return;
}
default: {
// Other errors such as network errors, see HTTPClientErrors for more details
throw err;
}
}
Expand All @@ -486,7 +478,17 @@ run();

```

Validation errors can also occur when either method arguments or data returned from the server do not match the expected format. The `SDKValidationError` that is thrown as a result will capture the raw value that failed validation in an attribute called `rawValue`. Additionally, a `pretty()` method is available on this error that can be used to log a nicely formatted string since validation errors can list many issues and the plain error string may be difficult read when debugging.
Validation errors can also occur when either method arguments or data returned from the server do not match the expected format. The `SDKValidationError` that is thrown as a result will capture the raw value that failed validation in an attribute called `rawValue`. Additionally, a `pretty()` method is available on this error that can be used to log a nicely formatted multi-line string since validation errors can list many issues and the plain error string may be difficult read when debugging.

In some rare cases, the SDK can fail to get a response from the server or even make the request due to unexpected circumstances such as network conditions. These types of errors are captured in the `models/errors/httpclienterrors.ts` module:

| HTTP Client Error | Description |
| ---------------------------------------------------- | ---------------------------------------------------- |
| RequestAbortedError | HTTP request was aborted by the client |
| RequestTimeoutError | HTTP request timed out due to an AbortSignal signal |
| ConnectionError | HTTP client was unable to make a request to a server |
| InvalidRequestError | Any input used to create a request is invalid |
| UnexpectedClientError | Unrecognised or unexpected error |
<!-- End Error Handling [errors] -->

<!-- Start Server Selection [server] -->
Expand Down
3 changes: 2 additions & 1 deletion docs/models/components/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ let value: Errors = {
"Field is required",
"Invalid format",
],
value: "xx xx xx ",
};
```

Expand All @@ -18,4 +19,4 @@ let value: Errors = {
| Field | Type | Required | Description |
| ------------------------------------ | ------------------------------------ | ------------------------------------ | ------------------------------------ |
| `messages` | *string*[] | :heavy_check_mark: | N/A |
| `value` | *components.ValidationErrorDtoValue* | :heavy_minus_sign: | N/A |
| `value` | *components.ValidationErrorDtoValue* | :heavy_check_mark: | N/A |
Loading

0 comments on commit 6551289

Please sign in to comment.