Skip to content

Commit

Permalink
fix: update api and remove enum (#259)
Browse files Browse the repository at this point in the history
  • Loading branch information
wtrocki authored Jan 19, 2022
1 parent 92c7e21 commit 63f51d1
Show file tree
Hide file tree
Showing 17 changed files with 202 additions and 121 deletions.
8 changes: 5 additions & 3 deletions examples/src/connector.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// For released version replace with "@rhoas/connector-management-sdk"
import { Configuration, ConnectorsApi, APIErrorCodes } from "../../packages/connector-management-sdk";
import { Configuration, ConnectorsApi, APIErrorCodes, isServiceApiError } from "../../packages/connector-management-sdk";

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

10 changes: 7 additions & 3 deletions examples/src/kafka.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// For released version replace with "@rhoas/kafka-management-sdk"
import { Configuration, DefaultApi, APIErrorCodes } from "../../packages/kafka-management-sdk/dist";
import { Configuration, DefaultApi, APIErrorCodes, isServiceApiError } from "../../packages/kafka-management-sdk/dist";

const accessToken = process.env.CLOUD_API_TOKEN;
const basePath = "https://api.openshift.com";
Expand All @@ -14,7 +14,11 @@ const kafkaApi = new DefaultApi(apiConfig)
kafkaApi.getKafkas().then((data) => {
console.log(data?.data.items)
}).catch((err) => {
console.error(err.message)
console.error("Validation issue", err.code == APIErrorCodes.ERROR_8)
if (isServiceApiError(err)) {
console.error("Validation issue", err.response?.data.code == APIErrorCodes.ERROR_8)
}
console.error(err)
})



2 changes: 1 addition & 1 deletion examples/src/serviceAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ serviceAccountAPI.getServiceAccounts().then((data) => {
console.log(data?.data.items)
}).catch((err) => {
console.error(err.message)
console.error("Service account fail error", err.code == APIErrorCodes.ERROR_111)
console.error("Service account fail error", err.response?.data.code == APIErrorCodes.ERROR_111)
})

8 changes: 5 additions & 3 deletions examples/src/serviceRegistry.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Configuration, RegistriesApi, APIErrorCodes } from "../../packages/registry-management-sdk/dist";
import { Configuration, RegistriesApi, APIErrorCodes, isServiceApiError } from "../../packages/registry-management-sdk/dist";

const accessToken = process.env.CLOUD_API_TOKEN;
const basePath = "https://api.openshift.com";
Expand All @@ -13,7 +13,9 @@ const registryApi = new RegistriesApi(apiConfig)
registryApi.getRegistries().then((data) => {
console.log(data?.data)
}).catch((err) => {
console.error(err.message)
console.error("Invalid JSON format",err.code == APIErrorCodes.ERROR_5)
if (isServiceApiError(err)) {
console.error("Invalid JSON format", err.response?.data.code == APIErrorCodes.ERROR_5)
}
console.error(err)
})

7 changes: 4 additions & 3 deletions packages/connector-management-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ npm install @rhoas/connector-management-sdk --save
#### Usage

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

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

Expand Down
22 changes: 22 additions & 0 deletions packages/connector-management-sdk/src/errorHelpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { AxiosError } from "axios";
import { ModelError } from "./generated";

/**
* Check if the error code originates from the API
*
* @param error generic error returned from fumction
* @returns true if error originated from the API
*/
export const isServiceApiError = (error: unknown): error is AxiosError<ModelError> => {
return (error as AxiosError<ModelError>).response?.data.code !== undefined;
};

/**
* Get the error code from the API error
*
* @param error generic error returned from fumction
* @returns error code (one of fields of APIErrorCodes)
*/
export const getErrorCode = (error: unknown): string | undefined => {
return (error as AxiosError<ModelError>).response?.data?.code;
};
84 changes: 42 additions & 42 deletions packages/connector-management-sdk/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,131 +6,131 @@
apiCall.then((data) => {
console.log(data?.data.items)
}).catch((err) => {
if(APIErrorCodes.ERROR_5 == err.code) {
if(APIErrorCodes.ERROR_5 == err.response?.data.code) {
// Handle error
}
})
```
*/
export enum APIErrorCodes {
export const APIErrorCodes = {
/** Forbidden to perform this action*/
ERROR_4 = "KAFKAS-MGMT-4",
ERROR_4 : "KAFKAS-MGMT-4",

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

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

/** Resource not found*/
ERROR_7 = "KAFKAS-MGMT-7",
ERROR_7 : "KAFKAS-MGMT-7",

/** General validation failure*/
ERROR_8 = "KAFKAS-MGMT-8",
ERROR_8 : "KAFKAS-MGMT-8",

/** Unspecified error*/
ERROR_9 = "KAFKAS-MGMT-9",
ERROR_9 : "KAFKAS-MGMT-9",

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

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

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

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

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

/** Bad request*/
ERROR_21 = "KAFKAS-MGMT-21",
ERROR_21 : "KAFKAS-MGMT-21",

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

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

/** Resource gone*/
ERROR_25 = "KAFKAS-MGMT-25",
ERROR_25 : "KAFKAS-MGMT-25",

/** Provider not supported*/
ERROR_30 = "KAFKAS-MGMT-30",
ERROR_30 : "KAFKAS-MGMT-30",

/** Region not supported*/
ERROR_31 = "KAFKAS-MGMT-31",
ERROR_31 : "KAFKAS-MGMT-31",

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

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

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

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

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

/** Field validation failed*/
ERROR_37 = "KAFKAS-MGMT-37",
ERROR_37 : "KAFKAS-MGMT-37",

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

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

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

/** Instance Type not supported*/
ERROR_41 = "KAFKAS-MGMT-41",
ERROR_41 : "KAFKAS-MGMT-41",

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

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

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

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

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

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

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

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

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

/** Insufficient quota*/
ERROR_120 = "KAFKAS-MGMT-120",
ERROR_120 : "KAFKAS-MGMT-120",

/** Failed to check quota*/
ERROR_121 = "KAFKAS-MGMT-121",
ERROR_121 : "KAFKAS-MGMT-121",

/** Too Many requests*/
ERROR_429 = "KAFKAS-MGMT-429",
ERROR_429 : "KAFKAS-MGMT-429",

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

}
3 changes: 2 additions & 1 deletion packages/connector-management-sdk/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./generated";
export * from "./errors";
export * from "./errors";
export * from "./errorHelpers";
7 changes: 4 additions & 3 deletions packages/kafka-management-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ npm install @rhoas/kafka-management-sdk --save
#### Usage

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

const accessToken = process.env.CLOUD_API_TOKEN;
const basePath = "https://api.openshift.com";
Expand All @@ -31,8 +31,9 @@ kafkaApi
console.log(data?.data.items);
})
.catch((err) => {
console.error(err.message);
console.error("Validation issue", err.code == APIErrorCodes.ERROR_8)
if(isServiceApiError(err)){
console.error("Validation issue", getErrorCode(err) == APIErrorCodes.ERROR_8)
}
});
```

Expand Down
22 changes: 22 additions & 0 deletions packages/kafka-management-sdk/src/errorHelpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { AxiosError } from "axios";
import { ModelError } from "./generated";

/**
* Check if the error code originates from the API
*
* @param error generic error returned from fumction
* @returns true if error originated from the API
*/
export const isServiceApiError = (error: unknown): error is AxiosError<ModelError> => {
return (error as AxiosError<ModelError>).response?.data.code !== undefined;
};

/**
* Get the error code from the API error
*
* @param error generic error returned from fumction
* @returns error code (one of fields of APIErrorCodes)
*/
export const getErrorCode = (error: unknown): string | undefined => {
return (error as AxiosError<ModelError>).response?.data?.code;
};
Loading

0 comments on commit 63f51d1

Please sign in to comment.