@hashgraph/solo
Version:
An opinionated CLI tool to deploy and manage private Hedera Networks.
59 lines • 3.09 kB
JavaScript
// SPDX-License-Identifier: Apache-2.0
import { ResourceNotFoundError } from './errors/resource-operation-errors.js';
import { StatusCodes } from 'http-status-codes';
import { KubeApiError } from './errors/kube-api-error.js';
import { container } from 'tsyringe-neo';
import { InjectTokens } from '../../core/dependency-injection/inject-tokens.js';
export class KubeApiResponse {
/**
* Checks the response for an error status code to determine which error should be thrown.
*
* @param errorResponse - the error response returned from the Kubernetes API call.
* @param resourceType - the type of resource being checked.
* @param resourceOperation - the operation being performed on the resource.
* @param namespace - the namespace of the resource being checked.
* @param name - the name of the resource being checked.
*/
static throwError(errorResponse, resourceOperation, resourceType, namespace, name) {
if (KubeApiResponse.isNotFound(errorResponse)) {
throw new ResourceNotFoundError(resourceOperation, resourceType, namespace, name);
}
const isDeveloperMode = container.resolve(InjectTokens.SoloLogger).isDevMode();
if (KubeApiResponse.isFailingStatus(errorResponse)) {
throw new KubeApiError(`failed to ${resourceOperation} ${resourceType} '${name}' in namespace '${namespace}'`, errorResponse?.code || errorResponse?.statusCode, errorResponse?.input, isDeveloperMode ? errorResponse : undefined, {
resourceType: resourceType,
resourceOperation: resourceOperation,
namespace: namespace,
name: name,
});
}
throw new KubeApiError(`error occurred during ${resourceOperation} ${resourceType} '${name}' in namespace '${namespace}'`, errorResponse?.code || errorResponse?.statusCode, errorResponse?.input, isDeveloperMode ? errorResponse : undefined, {
resourceType: resourceType,
resourceOperation: resourceOperation,
namespace: namespace,
name: name,
});
}
/**
* Checks if the error response has a status code indicating a failing status (greater than 202 Accepted).
* @param errorResponse
*/
static isFailingStatus(errorResponse) {
return ((errorResponse?.code || errorResponse?.statusCode || StatusCodes.INTERNAL_SERVER_ERROR) > StatusCodes.ACCEPTED);
}
/**
* Checks if the error response has a status code indicating a "Not Found" error (404).
* @param errorResponse
*/
static isNotFound(errorResponse) {
return errorResponse?.code === StatusCodes.NOT_FOUND || errorResponse?.statusCode === StatusCodes.NOT_FOUND;
}
/**
* Checks if the error response has a status code indicating a "Created" status (201).
* @param errorResponse
*/
static isCreatedStatus(errorResponse) {
return errorResponse?.code === StatusCodes.CREATED || errorResponse?.statusCode === StatusCodes.CREATED;
}
}
//# sourceMappingURL=kube-api-response.js.map