Skip to content

Commit 63f51d1

Browse files
authored
fix: update api and remove enum (#259)
1 parent 92c7e21 commit 63f51d1

File tree

17 files changed

+202
-121
lines changed

17 files changed

+202
-121
lines changed

examples/src/connector.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// For released version replace with "@rhoas/connector-management-sdk"
2-
import { Configuration, ConnectorsApi, APIErrorCodes } from "../../packages/connector-management-sdk";
2+
import { Configuration, ConnectorsApi, APIErrorCodes, isServiceApiError } from "../../packages/connector-management-sdk";
33

44
const accessToken = process.env.CLOUD_API_TOKEN;
55
const basePath = "https://api.openshift.com";
@@ -14,7 +14,9 @@ const connectorsApi = new ConnectorsApi(apiConfig)
1414
connectorsApi.getConnector("id", "kafka-id").then((data) => {
1515
console.log(data?.data)
1616
}).catch((err) => {
17-
console.error(err.message)
18-
console.error("Validation issue", err.code == APIErrorCodes.ERROR_8)
17+
if (isServiceApiError(err)) {
18+
console.error("Validation issue", err.response?.data.code == APIErrorCodes.ERROR_8)
19+
}
20+
console.error(err)
1921
})
2022

examples/src/kafka.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// For released version replace with "@rhoas/kafka-management-sdk"
2-
import { Configuration, DefaultApi, APIErrorCodes } from "../../packages/kafka-management-sdk/dist";
2+
import { Configuration, DefaultApi, APIErrorCodes, isServiceApiError } from "../../packages/kafka-management-sdk/dist";
33

44
const accessToken = process.env.CLOUD_API_TOKEN;
55
const basePath = "https://api.openshift.com";
@@ -14,7 +14,11 @@ const kafkaApi = new DefaultApi(apiConfig)
1414
kafkaApi.getKafkas().then((data) => {
1515
console.log(data?.data.items)
1616
}).catch((err) => {
17-
console.error(err.message)
18-
console.error("Validation issue", err.code == APIErrorCodes.ERROR_8)
17+
if (isServiceApiError(err)) {
18+
console.error("Validation issue", err.response?.data.code == APIErrorCodes.ERROR_8)
19+
}
20+
console.error(err)
1921
})
2022

23+
24+

examples/src/serviceAccount.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ serviceAccountAPI.getServiceAccounts().then((data) => {
1414
console.log(data?.data.items)
1515
}).catch((err) => {
1616
console.error(err.message)
17-
console.error("Service account fail error", err.code == APIErrorCodes.ERROR_111)
17+
console.error("Service account fail error", err.response?.data.code == APIErrorCodes.ERROR_111)
1818
})
1919

examples/src/serviceRegistry.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Configuration, RegistriesApi, APIErrorCodes } from "../../packages/registry-management-sdk/dist";
1+
import { Configuration, RegistriesApi, APIErrorCodes, isServiceApiError } from "../../packages/registry-management-sdk/dist";
22

33
const accessToken = process.env.CLOUD_API_TOKEN;
44
const basePath = "https://api.openshift.com";
@@ -13,7 +13,9 @@ const registryApi = new RegistriesApi(apiConfig)
1313
registryApi.getRegistries().then((data) => {
1414
console.log(data?.data)
1515
}).catch((err) => {
16-
console.error(err.message)
17-
console.error("Invalid JSON format",err.code == APIErrorCodes.ERROR_5)
16+
if (isServiceApiError(err)) {
17+
console.error("Invalid JSON format", err.response?.data.code == APIErrorCodes.ERROR_5)
18+
}
19+
console.error(err)
1820
})
1921

packages/connector-management-sdk/README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ npm install @rhoas/connector-management-sdk --save
1313
#### Usage
1414

1515
```ts
16-
import { Configuration, ConnectorsApi, APIErrorCodes } from "@rhoas/connector-management-sdk";
16+
import { Configuration, ConnectorsApi, getErrorCode, isServiceApiError, APIErrorCodes } from "@rhoas/connector-management-sdk";
1717

1818
const accessToken = process.env.CLOUD_API_TOKEN;
1919
const basePath = "https://api.openshift.com";
@@ -28,8 +28,9 @@ const connectorsApi = new ConnectorsApi(apiConfig)
2828
connectorsApi.getConnector("id", "kafka-id").then((data) => {
2929
console.log(data?.data)
3030
}).catch((err) => {
31-
console.error(err.message)
32-
console.error("Validation issue", err.code == APIErrorCodes.ERROR_8)
31+
if(isServiceApiError(err)){
32+
console.error("Validation issue", getErrorCode(err) == APIErrorCodes.ERROR_8)
33+
}
3334
})
3435
```
3536

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { AxiosError } from "axios";
2+
import { ModelError } from "./generated";
3+
4+
/**
5+
* Check if the error code originates from the API
6+
*
7+
* @param error generic error returned from fumction
8+
* @returns true if error originated from the API
9+
*/
10+
export const isServiceApiError = (error: unknown): error is AxiosError<ModelError> => {
11+
return (error as AxiosError<ModelError>).response?.data.code !== undefined;
12+
};
13+
14+
/**
15+
* Get the error code from the API error
16+
*
17+
* @param error generic error returned from fumction
18+
* @returns error code (one of fields of APIErrorCodes)
19+
*/
20+
export const getErrorCode = (error: unknown): string | undefined => {
21+
return (error as AxiosError<ModelError>).response?.data?.code;
22+
};

packages/connector-management-sdk/src/errors.ts

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,131 +6,131 @@
66
apiCall.then((data) => {
77
console.log(data?.data.items)
88
}).catch((err) => {
9-
if(APIErrorCodes.ERROR_5 == err.code) {
9+
if(APIErrorCodes.ERROR_5 == err.response?.data.code) {
1010
// Handle error
1111
}
1212
})
1313
```
1414
*/
15-
export enum APIErrorCodes {
15+
export const APIErrorCodes = {
1616
/** Forbidden to perform this action*/
17-
ERROR_4 = "KAFKAS-MGMT-4",
17+
ERROR_4 : "KAFKAS-MGMT-4",
1818

1919
/** Forbidden to create more instances than the maximum allowed*/
20-
ERROR_5 = "KAFKAS-MGMT-5",
20+
ERROR_5 : "KAFKAS-MGMT-5",
2121

2222
/** An entity with the specified unique values already exists*/
23-
ERROR_6 = "KAFKAS-MGMT-6",
23+
ERROR_6 : "KAFKAS-MGMT-6",
2424

2525
/** Resource not found*/
26-
ERROR_7 = "KAFKAS-MGMT-7",
26+
ERROR_7 : "KAFKAS-MGMT-7",
2727

2828
/** General validation failure*/
29-
ERROR_8 = "KAFKAS-MGMT-8",
29+
ERROR_8 : "KAFKAS-MGMT-8",
3030

3131
/** Unspecified error*/
32-
ERROR_9 = "KAFKAS-MGMT-9",
32+
ERROR_9 : "KAFKAS-MGMT-9",
3333

3434
/** HTTP Method not implemented for this endpoint*/
35-
ERROR_10 = "KAFKAS-MGMT-10",
35+
ERROR_10 : "KAFKAS-MGMT-10",
3636

3737
/** Account is unauthorized to perform this action*/
38-
ERROR_11 = "KAFKAS-MGMT-11",
38+
ERROR_11 : "KAFKAS-MGMT-11",
3939

4040
/** Required terms have not been accepted*/
41-
ERROR_12 = "KAFKAS-MGMT-12",
41+
ERROR_12 : "KAFKAS-MGMT-12",
4242

4343
/** Account authentication could not be verified*/
44-
ERROR_15 = "KAFKAS-MGMT-15",
44+
ERROR_15 : "KAFKAS-MGMT-15",
4545

4646
/** Unable to read request body*/
47-
ERROR_17 = "KAFKAS-MGMT-17",
47+
ERROR_17 : "KAFKAS-MGMT-17",
4848

4949
/** Bad request*/
50-
ERROR_21 = "KAFKAS-MGMT-21",
50+
ERROR_21 : "KAFKAS-MGMT-21",
5151

5252
/** Failed to parse search query*/
53-
ERROR_23 = "KAFKAS-MGMT-23",
53+
ERROR_23 : "KAFKAS-MGMT-23",
5454

5555
/** The maximum number of allowed kafka instances has been reached*/
56-
ERROR_24 = "KAFKAS-MGMT-24",
56+
ERROR_24 : "KAFKAS-MGMT-24",
5757

5858
/** Resource gone*/
59-
ERROR_25 = "KAFKAS-MGMT-25",
59+
ERROR_25 : "KAFKAS-MGMT-25",
6060

6161
/** Provider not supported*/
62-
ERROR_30 = "KAFKAS-MGMT-30",
62+
ERROR_30 : "KAFKAS-MGMT-30",
6363

6464
/** Region not supported*/
65-
ERROR_31 = "KAFKAS-MGMT-31",
65+
ERROR_31 : "KAFKAS-MGMT-31",
6666

6767
/** Kafka cluster name is invalid*/
68-
ERROR_32 = "KAFKAS-MGMT-32",
68+
ERROR_32 : "KAFKAS-MGMT-32",
6969

7070
/** Minimum field length not reached*/
71-
ERROR_33 = "KAFKAS-MGMT-33",
71+
ERROR_33 : "KAFKAS-MGMT-33",
7272

7373
/** Maximum field length has been depassed*/
74-
ERROR_34 = "KAFKAS-MGMT-34",
74+
ERROR_34 : "KAFKAS-MGMT-34",
7575

7676
/** Only multiAZ Kafkas are supported, use multi_az=true*/
77-
ERROR_35 = "KAFKAS-MGMT-35",
77+
ERROR_35 : "KAFKAS-MGMT-35",
7878

7979
/** Kafka cluster name is already used*/
80-
ERROR_36 = "KAFKAS-MGMT-36",
80+
ERROR_36 : "KAFKAS-MGMT-36",
8181

8282
/** Field validation failed*/
83-
ERROR_37 = "KAFKAS-MGMT-37",
83+
ERROR_37 : "KAFKAS-MGMT-37",
8484

8585
/** Service account name is invalid*/
86-
ERROR_38 = "KAFKAS-MGMT-38",
86+
ERROR_38 : "KAFKAS-MGMT-38",
8787

8888
/** Service account desc is invalid*/
89-
ERROR_39 = "KAFKAS-MGMT-39",
89+
ERROR_39 : "KAFKAS-MGMT-39",
9090

9191
/** Service account id is invalid*/
92-
ERROR_40 = "KAFKAS-MGMT-40",
92+
ERROR_40 : "KAFKAS-MGMT-40",
9393

9494
/** Instance Type not supported*/
95-
ERROR_41 = "KAFKAS-MGMT-41",
95+
ERROR_41 : "KAFKAS-MGMT-41",
9696

9797
/** Synchronous action is not supported, use async=true parameter*/
98-
ERROR_103 = "KAFKAS-MGMT-103",
98+
ERROR_103 : "KAFKAS-MGMT-103",
9999

100100
/** Failed to create kafka client in the mas sso*/
101-
ERROR_106 = "KAFKAS-MGMT-106",
101+
ERROR_106 : "KAFKAS-MGMT-106",
102102

103103
/** Failed to get kafka client secret from the mas sso*/
104-
ERROR_107 = "KAFKAS-MGMT-107",
104+
ERROR_107 : "KAFKAS-MGMT-107",
105105

106106
/** Failed to get kafka client from the mas sso*/
107-
ERROR_108 = "KAFKAS-MGMT-108",
107+
ERROR_108 : "KAFKAS-MGMT-108",
108108

109109
/** Failed to delete kafka client from the mas sso*/
110-
ERROR_109 = "KAFKAS-MGMT-109",
110+
ERROR_109 : "KAFKAS-MGMT-109",
111111

112112
/** Failed to create service account*/
113-
ERROR_110 = "KAFKAS-MGMT-110",
113+
ERROR_110 : "KAFKAS-MGMT-110",
114114

115115
/** Failed to get service account*/
116-
ERROR_111 = "KAFKAS-MGMT-111",
116+
ERROR_111 : "KAFKAS-MGMT-111",
117117

118118
/** Failed to delete service account*/
119-
ERROR_112 = "KAFKAS-MGMT-112",
119+
ERROR_112 : "KAFKAS-MGMT-112",
120120

121121
/** Failed to find service account*/
122-
ERROR_113 = "KAFKAS-MGMT-113",
122+
ERROR_113 : "KAFKAS-MGMT-113",
123123

124124
/** Insufficient quota*/
125-
ERROR_120 = "KAFKAS-MGMT-120",
125+
ERROR_120 : "KAFKAS-MGMT-120",
126126

127127
/** Failed to check quota*/
128-
ERROR_121 = "KAFKAS-MGMT-121",
128+
ERROR_121 : "KAFKAS-MGMT-121",
129129

130130
/** Too Many requests*/
131-
ERROR_429 = "KAFKAS-MGMT-429",
131+
ERROR_429 : "KAFKAS-MGMT-429",
132132

133133
/** An unexpected error happened, please check the log of the service for details*/
134-
ERROR_1000 = "KAFKAS-MGMT-1000",
134+
ERROR_1000 : "KAFKAS-MGMT-1000",
135135

136136
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from "./generated";
2-
export * from "./errors";
2+
export * from "./errors";
3+
export * from "./errorHelpers";

packages/kafka-management-sdk/README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ npm install @rhoas/kafka-management-sdk --save
1313
#### Usage
1414

1515
```ts
16-
import { Configuration, DefaultApi, APIErrorCodes } from "@rhoas/kafka-management-sdk";
16+
import { Configuration, DefaultApi, getErrorCode, isServiceApiError, APIErrorCodes } from "@rhoas/kafka-management-sdk";
1717

1818
const accessToken = process.env.CLOUD_API_TOKEN;
1919
const basePath = "https://api.openshift.com";
@@ -31,8 +31,9 @@ kafkaApi
3131
console.log(data?.data.items);
3232
})
3333
.catch((err) => {
34-
console.error(err.message);
35-
console.error("Validation issue", err.code == APIErrorCodes.ERROR_8)
34+
if(isServiceApiError(err)){
35+
console.error("Validation issue", getErrorCode(err) == APIErrorCodes.ERROR_8)
36+
}
3637
});
3738
```
3839

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { AxiosError } from "axios";
2+
import { ModelError } from "./generated";
3+
4+
/**
5+
* Check if the error code originates from the API
6+
*
7+
* @param error generic error returned from fumction
8+
* @returns true if error originated from the API
9+
*/
10+
export const isServiceApiError = (error: unknown): error is AxiosError<ModelError> => {
11+
return (error as AxiosError<ModelError>).response?.data.code !== undefined;
12+
};
13+
14+
/**
15+
* Get the error code from the API error
16+
*
17+
* @param error generic error returned from fumction
18+
* @returns error code (one of fields of APIErrorCodes)
19+
*/
20+
export const getErrorCode = (error: unknown): string | undefined => {
21+
return (error as AxiosError<ModelError>).response?.data?.code;
22+
};

0 commit comments

Comments
 (0)