Skip to content

Commit

Permalink
chore: use options arg for handleError sig, for readability
Browse files Browse the repository at this point in the history
  • Loading branch information
cprice404 committed Jan 3, 2024
1 parent db594e6 commit 799727f
Show file tree
Hide file tree
Showing 19 changed files with 1,050 additions and 1,010 deletions.
22 changes: 12 additions & 10 deletions packages/client-sdk-nodejs/src/errors/cache-service-error-mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,27 @@ import {
FailedPreconditionError,
} from '../../src';

export interface HandleCacheServiceErrorOptions {
err: ServiceError | null;
errorResponseFactoryFn: (err: SdkError) => unknown;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
resolveFn: (result: any) => void;
rejectFn: (err: SdkError) => void;
}

export class CacheServiceErrorMapper {
private readonly throwOnError: boolean;

constructor(throwOnError: boolean) {
this.throwOnError = throwOnError;
}
handleError(
err: ServiceError | null,
errorResponseFactoryFn: (err: SdkError) => unknown,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
resolveFn: (result: any) => void,
rejectFn: (err: SdkError) => void
): void {
const error = this.convertError(err);
handleError(opts: HandleCacheServiceErrorOptions): void {
const error = this.convertError(opts.err);

if (this.throwOnError) {
rejectFn(error);
opts.rejectFn(error);
} else {
resolveFn(errorResponseFactoryFn(error));
opts.resolveFn(opts.errorResponseFactoryFn(error));
}
}

Expand Down
84 changes: 42 additions & 42 deletions packages/client-sdk-nodejs/src/internal/cache-control-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,12 @@ export class CacheControlClient {
if (err.code === Status.ALREADY_EXISTS) {
resolve(new CreateCache.AlreadyExists());
} else {
this.cacheServiceErrorMapper.handleError(
err,
e => new CreateCache.Error(e),
resolve,
reject
);
this.cacheServiceErrorMapper.handleError({
err: err,
errorResponseFactoryFn: e => new CreateCache.Error(e),
resolveFn: resolve,
rejectFn: reject,
});
}
} else {
resolve(new CreateCache.Success());
Expand Down Expand Up @@ -130,12 +130,12 @@ export class CacheControlClient {
{interceptors: this.interceptors},
(err, _resp) => {
if (err) {
this.cacheServiceErrorMapper.handleError(
err,
e => new DeleteCache.Error(e),
resolve,
reject
);
this.cacheServiceErrorMapper.handleError({
err: err,
errorResponseFactoryFn: e => new DeleteCache.Error(e),
resolveFn: resolve,
rejectFn: reject,
});
} else {
resolve(new DeleteCache.Success());
}
Expand Down Expand Up @@ -170,12 +170,12 @@ export class CacheControlClient {
if (resp) {
resolve(new CacheFlush.Success());
} else {
this.cacheServiceErrorMapper.handleError(
err,
e => new CacheFlush.Error(e),
resolve,
reject
);
this.cacheServiceErrorMapper.handleError({
err: err,
errorResponseFactoryFn: e => new CacheFlush.Error(e),
resolveFn: resolve,
rejectFn: reject,
});
}
}
);
Expand All @@ -191,12 +191,12 @@ export class CacheControlClient {
.getClient()
.ListCaches(request, {interceptors: this.interceptors}, (err, resp) => {
if (err || !resp) {
this.cacheServiceErrorMapper.handleError(
err,
e => new ListCaches.Error(e),
resolve,
reject
);
this.cacheServiceErrorMapper.handleError({
err: err,
errorResponseFactoryFn: e => new ListCaches.Error(e),
resolveFn: resolve,
rejectFn: reject,
});
} else {
const caches = resp.cache.map(cache => {
const cacheName = cache.cache_name;
Expand Down Expand Up @@ -241,12 +241,12 @@ export class CacheControlClient {
{interceptors: this.interceptors},
(err, resp) => {
if (err) {
this.cacheServiceErrorMapper.handleError(
err,
e => new CreateSigningKey.Error(e),
resolve,
reject
);
this.cacheServiceErrorMapper.handleError({
err: err,
errorResponseFactoryFn: e => new CreateSigningKey.Error(e),
resolveFn: resolve,
rejectFn: reject,
});
} else {
const signingKey = new _SigningKey(resp?.key, resp?.expires_at);
resolve(new CreateSigningKey.Success(endpoint, signingKey));
Expand All @@ -267,12 +267,12 @@ export class CacheControlClient {
.getClient()
.RevokeSigningKey(request, {interceptors: this.interceptors}, err => {
if (err) {
this.cacheServiceErrorMapper.handleError(
err,
e => new RevokeSigningKey.Error(e),
resolve,
reject
);
this.cacheServiceErrorMapper.handleError({
err: err,
errorResponseFactoryFn: e => new RevokeSigningKey.Error(e),
resolveFn: resolve,
rejectFn: reject,
});
} else {
resolve(new RevokeSigningKey.Success());
}
Expand All @@ -294,12 +294,12 @@ export class CacheControlClient {
{interceptors: this.interceptors},
(err, resp) => {
if (err || !resp) {
this.cacheServiceErrorMapper.handleError(
err,
e => new ListSigningKeys.Error(e),
resolve,
reject
);
this.cacheServiceErrorMapper.handleError({
err: err,
errorResponseFactoryFn: e => new ListSigningKeys.Error(e),
resolveFn: resolve,
rejectFn: reject,
});
} else {
const signingKeys = resp.signing_key.map(
sk => new _SigningKey(sk.key_id, sk.expires_at)
Expand Down
Loading

0 comments on commit 799727f

Please sign in to comment.