Skip to content

Commit 711c427

Browse files
Improve custom ShapeDiver error objects
1 parent 78cc66b commit 711c427

File tree

6 files changed

+57
-44
lines changed

6 files changed

+57
-44
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
"downstream": "bash ./scripts/downstream.sh"
2121
},
2222
"devDependencies": {
23-
"axios-mock-adapter": "~2.1.0",
2423
"eslint": "~9.12.0",
2524
"@eslint/js": "~9.12.0",
2625
"eslint-plugin-jest": "~28.8.3",

packages/sdk.geometry-api-sdk-v2/README.md

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -108,30 +108,44 @@ The SDK provides a helper function to extract ShapeDiver error information from
108108
```typescript
109109
import {
110110
processError,
111-
SdError,
112-
SdRequestError,
113-
SdResponseError,
111+
SdGeometryError,
112+
RequestError,
113+
ResponseError,
114114
} from "@shapediver/sdk.geometry-api-sdk-v2"
115115

116116
try {
117117
sdk.model.get("be5d4ce5-f76d-417d-8496-1f038e6f0cab")
118-
catch (err) {
118+
} catch (err) {
119119
const e = processError(err);
120120

121-
if (e instanceof SdRequestError) {
122-
// e is a request error.
123-
// In this case, the request was made but no response was received.
121+
if (e instanceof SdGeometryError) {
122+
/*
123+
* Generic ShapeDiver error.
124+
*
125+
* Generic errors are the base class of all custom ShapeDiver errors, like RequestError,
126+
* ResponseError, IllegalArgumentError, TimeoutError, etc.
127+
*
128+
* Warning:
129+
* Generic Axios errors (non-request/response errors) that are thrown when setting up the
130+
* request are not converted into a SdGeometryError!
131+
*/
124132
}
125133

126-
if (e instanceof SdResponseError) {
127-
// e is a response error.
128-
// In this case, the request was made and the server responded with a status code that falls
129-
// out of the range of 2xx.
134+
if (e instanceof RequestError) {
135+
/*
136+
* Wrapper around an Axios request error.
137+
*
138+
* In this case, the request was made but no response was received.
139+
*/
130140
}
131141

132-
if (e instanceof SdError) {
133-
// e is a generic error.
134-
// Generic errors are used for everything that is neither a request error nor a response error.
142+
if (e instanceof ResponseError) {
143+
/*
144+
* Wrapper around an Axios response error.
145+
*
146+
* In this case, the request was made and the server responded with a status code that falls
147+
* out of the range of 2xx.
148+
*/
135149
}
136150
}
137151
```

packages/sdk.geometry-api-sdk-v2/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"axios": "~1.7.7"
4747
},
4848
"devDependencies": {
49+
"axios-mock-adapter": "~2.1.0",
4950
"jest": "~29.7.0",
5051
"lerna": "8.1.8",
5152
"typescript": "~5.5.4",

packages/sdk.geometry-api-sdk-v2/src/error.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import { ResErrorType } from './client';
22

33
/**
4-
* Represents a generic (non-request and non-response) error.
4+
* Represents a generic ShapeDiver error.
55
*
66
* Something happened in setting up the request that triggered an Error.
77
* @export
8-
* @class SdError
8+
* @class SdGeometryError
99
* @extends {Error}
1010
*/
11-
export class SdError extends Error {
12-
constructor(message: string) {
11+
export class SdGeometryError extends Error {
12+
constructor(message?: string) {
1313
super(message);
14-
this.name = 'SdError';
14+
this.name = 'SdGeometryError';
1515
}
1616
}
1717

@@ -20,13 +20,13 @@ export class SdError extends Error {
2020
*
2121
* The request was made but no response was received.
2222
* @export
23-
* @class SdRequestError
24-
* @extends {Error}
23+
* @class RequestError
24+
* @extends {SdGeometryError}
2525
*/
26-
export class SdRequestError extends Error {
26+
export class RequestError extends SdGeometryError {
2727
constructor(message: string) {
2828
super(message);
29-
this.name = 'SdRequestError';
29+
this.name = 'RequestError';
3030
}
3131
}
3232

@@ -36,10 +36,10 @@ export class SdRequestError extends Error {
3636
* The request was made and the server responded with a status code that falls out of the range of
3737
* 2xx.
3838
* @export
39-
* @class SdResponseError
40-
* @extends {Error}
39+
* @class ResponseError
40+
* @extends {SdGeometryError}
4141
*/
42-
export class SdResponseError extends Error {
42+
export class ResponseError extends SdGeometryError {
4343
/** The HTTP status code from the server response. */
4444
public readonly status: number;
4545

@@ -51,7 +51,7 @@ export class SdResponseError extends Error {
5151

5252
constructor(status: number, message: string, desc: string, type?: string) {
5353
super(message);
54-
this.name = 'SdResponseError';
54+
this.name = 'ResponseError';
5555
this.status = status;
5656
this.description = desc;
5757
this.type =
@@ -64,9 +64,9 @@ export class SdResponseError extends Error {
6464
/**
6565
* @export
6666
* @class IllegalArgumentError
67-
* @extends {Error}
67+
* @extends {SdGeometryError}
6868
*/
69-
export class IllegalArgumentError extends Error {
69+
export class IllegalArgumentError extends SdGeometryError {
7070
constructor(
7171
public field: string,
7272
msg?: string
@@ -79,9 +79,9 @@ export class IllegalArgumentError extends Error {
7979
/**
8080
* @export
8181
* @class TimeoutError
82-
* @extends {Error}
82+
* @extends {SdGeometryError}
8383
*/
84-
export class TimeoutError extends Error {
84+
export class TimeoutError extends SdGeometryError {
8585
constructor(
8686
public field: string,
8787
msg?: string

packages/sdk.geometry-api-sdk-v2/src/utils.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { AxiosError, AxiosPromise } from 'axios';
2-
import { SdError, SdRequestError, SdResponseError } from './error';
2+
import { RequestError, ResponseError } from './error';
33

44
/** Delays the response for the given number of milliseconds */
55
export function sleep(ms: number): Promise<void> {
@@ -93,12 +93,11 @@ export async function exists(apiCall: () => AxiosPromise<unknown>): Promise<bool
9393
}
9494

9595
/**
96-
* Converts a generic Axios error into a more specific ShapeDiver error.
96+
* Tries to convert a generic Axios error into a more specific ShapeDiver error. When no match is
97+
* found, the original error is returned instead.
9798
* @param error The Axios error to convert.
9899
*/
99-
export function processError(
100-
error: AxiosError | Error
101-
): SdError | SdRequestError | SdResponseError {
100+
export function processError(error: AxiosError | Error): Error | RequestError | ResponseError {
102101
if ('response' in error) {
103102
const err = error as AxiosError,
104103
status = err.response!.status,
@@ -114,13 +113,13 @@ export function processError(
114113
'message' in data &&
115114
typeof data.message === 'string'
116115
) {
117-
return new SdResponseError(status, data.message, data.desc, data.error);
116+
return new ResponseError(status, data.message, data.desc, data.error);
118117
} else {
119-
return new SdResponseError(status, err.message, 'No error description provided');
118+
return new ResponseError(status, err.message, 'No error description provided');
120119
}
121120
} else if ('request' in error) {
122-
return new SdRequestError(error.message);
121+
return new RequestError(error.message);
123122
} else {
124-
return new SdError(error.message);
123+
return error;
125124
}
126125
}

pnpm-lock.yaml

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)