Skip to content

Commit

Permalink
fix: migrate to typescript 4.4
Browse files Browse the repository at this point in the history
  • Loading branch information
izatop committed Aug 29, 2021
1 parent 5f4e5f6 commit 4d993a1
Show file tree
Hide file tree
Showing 17 changed files with 408 additions and 348 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@
"@commitlint/cli": "^13.1.0",
"@commitlint/config-conventional": "^13.1.0",
"@types/jest": "^27.0.1",
"@types/node": "^16.7.2",
"@types/node": "^16.7.5",
"@typescript-eslint/eslint-plugin": "^4.29.3",
"@typescript-eslint/parser": "^4.29.3",
"cross-env": "^7.0.3",
"eslint": "^7.32.0",
"jest": "^27.0.6",
"jest": "^27.1.0",
"rimraf": "^3.0.2",
"ts-jest": "^27.0.5",
"typescript": "^4.3.5"
"typescript": "^4.4.2"
},
"resolutions": {
"minimist": "1.2.5"
Expand Down
2 changes: 1 addition & 1 deletion packages/fs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"dependencies": {
"@bunt/unit": "^0.19.12",
"@bunt/util": "^0.19.12",
"@types/minio": "^7.0.8",
"@types/minio": "^7.0.10",
"minio": "^7.0.19"
},
"license": "MIT"
Expand Down
3 changes: 2 additions & 1 deletion packages/fs/src/Storage/Driver/MinIO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export class MinIO extends FsDriverAbstract {
}

public write(bucket: string, name: string, file: string, metadata: Record<any, any>): Promise<string> {
return this.#client.fPutObject(bucket, name, file, metadata);
return this.#client.fPutObject(bucket, name, file, metadata)
.then((res) => res.etag);
}
}
4 changes: 2 additions & 2 deletions packages/input/src/Type/Fields.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {entriesReverse, isFunction, isInstanceOf, isObject} from "@bunt/util";
import {entriesReverse, isFunction, isInstanceOf, isObject, toError} from "@bunt/util";
import {AssertionObjectError, AssertionTypeError, IReadableTypeError} from "../Assertion";
import {ObjectFields, ObjectTypeMerge} from "../interfaces";
import {TypeAbstract} from "../TypeAbstract";
Expand Down Expand Up @@ -56,7 +56,7 @@ export class Fields<TValue extends Record<string, any>> extends TypeAbstract<TVa

validations.set(field, {
payload: payload[field],
message: error.message,
message: toError(error, "Unknown").message,
type: "Unknown",
});
}
Expand Down
4 changes: 2 additions & 2 deletions packages/input/src/Type/List.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {isArray, isInstanceOf} from "@bunt/util";
import {isArray, isInstanceOf, toError} from "@bunt/util";
import {AssertionListError, AssertionTypeError, IReadableListField} from "../Assertion";
import {SuperType} from "../SuperType";

Expand All @@ -19,7 +19,7 @@ export class List<TValue> extends SuperType<TValue[], TValue> {
validations.add({
index,
payload: item,
message: error.message,
message: toError(error, "Unknown").message,
type: this.type.name,
});
}
Expand Down
2 changes: 1 addition & 1 deletion packages/queue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"dependencies": {
"@bunt/unit": "^0.19.12",
"@bunt/util": "^0.19.12",
"@types/ioredis": "^4.27.0",
"@types/ioredis": "^4.27.1",
"ioredis": "^4.27.8"
},
"license": "MIT"
Expand Down
4 changes: 2 additions & 2 deletions packages/queue/src/Queue/QueueListAbstract.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Disposable} from "@bunt/unit";
import {assert, isDefined, isInstanceOf} from "@bunt/util";
import {assert, isDefined, isInstanceOf, toError} from "@bunt/util";
import {ITransport} from "../interfaces";
import {
IQueueList,
Expand Down Expand Up @@ -81,7 +81,7 @@ export abstract class QueueListAbstract<M extends Message> implements IQueueList

await this.fire(operation.commit());
} catch (error) {
await this.fire(operation.rollback(error));
await this.fire(operation.rollback(toError(error, "Unexpected error")));
}
}

Expand Down
3 changes: 2 additions & 1 deletion packages/queue/src/Queue/ReadOperation.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {toError} from "@bunt/util";
import {createReleaseState} from "./fn";
import {IHandleReleaseFactory, IReadOperation, IReadOperationFail, Message, OperationReleaseState} from "./interfaces";

Expand All @@ -22,7 +23,7 @@ export class ReadOperation<M extends Message> implements IReadOperation<M> {
try {
return this.release();
} catch (error) {
return this.release(error);
return this.release(toError(error, "Unexpected error"));
}
}

Expand Down
5 changes: 3 additions & 2 deletions packages/queue/src/Redis/RedisQ2ReadOperation.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {toError} from "@bunt/util";
import {IReadOperationFail, Message, OperationReleaseState, ReadOperation} from "../Queue";

export class RedisQ2ReadOperation<M extends Message> extends ReadOperation<M> {
Expand All @@ -14,7 +15,7 @@ export class RedisQ2ReadOperation<M extends Message> extends ReadOperation<M> {
try {
await this.#commit();
} catch (error) {
return this.release(error);
return this.release(toError(error, "Unknown"));
}

return this.release();
Expand All @@ -24,7 +25,7 @@ export class RedisQ2ReadOperation<M extends Message> extends ReadOperation<M> {
try {
await this.#rollback();
} catch (error) {
return this.release(error);
return this.release(toError(error, "Unexpected error"));
}

return this.release(reason ?? new Error("Unknown reason"));
Expand Down
2 changes: 1 addition & 1 deletion packages/unit/src/Runtime/Runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export class Runtime implements IDisposable {

await Promise.allSettled(this.queue.map((heartbeat) => heartbeat.watch()));
} catch (error) {
this.logger.emergency(error.message, error.stack);
this.logger.emergency("Unexpected error", error);
} finally {
finish();
}
Expand Down
2 changes: 1 addition & 1 deletion packages/unit/src/Runtime/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface IDisposable {
}

export interface IDisposedHistory {
error?: Error;
error?: Error | unknown;
label: string;
timeout: number;
target: string;
Expand Down
3 changes: 2 additions & 1 deletion packages/util/src/Logger/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ export type LogableType = string
| null
| undefined
| boolean
| Date;
| Date
| unknown;

export interface ILogable<T extends LogableType> {
getLogValue(): T;
Expand Down
12 changes: 12 additions & 0 deletions packages/util/src/assert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,15 @@ export function pass<T>(value: unknown, transform: (v: any) => T, validator: (v:

return nextValue;
}

export function toError(error: unknown, alt: Error | string = "Unexpected error"): Error {
if (error instanceof Error) {
return error;
}

if (typeof alt === "string") {
return new Error(alt);
}

return alt;
}
11 changes: 5 additions & 6 deletions packages/web/src/WebServer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {Application, IRoute} from "@bunt/app";
import {ActionAny, Context, ContextArg, Heartbeat, IDisposable, IRunnable, unit, Unit} from "@bunt/unit";
import {assert, Ctor, logger, Logger, PermissionError, ValidationError} from "@bunt/util";
import {NotFound} from "@bunt/util/dist/Exception/NotFound";
import {assert, Ctor, logger, Logger, PermissionError, toError, NotFound, ValidationError} from "@bunt/util";
import * as http from "http";
import {IncomingMessage, ServerResponse} from "http";
import {Socket} from "net";
Expand Down Expand Up @@ -86,7 +85,7 @@ export class WebServer<C extends Context> extends Application<C>
const response = await this.run(request);
await request.respond(response);
} catch (error) {
this.logger.error(error.message, error);
this.logger.error(toError(error).message, error);

await request.respond(error);
} finally {
Expand All @@ -104,9 +103,9 @@ export class WebServer<C extends Context> extends Application<C>
assert(acceptor, `Unsupported protocol ${protocol}`);
acceptor.handle(req, socket, head);
} catch (error) {
this.logger.warning(error.message, error);
this.logger.warning("Unexpected error", error);
socket.write(`HTTP/1.1 400 Bad request\r\nContent-Type: text/plain\r\nConnection: close\r\n\r\n`);
socket.destroy(error);
socket.destroy(toError(error));
}
};

Expand All @@ -120,7 +119,7 @@ export class WebServer<C extends Context> extends Application<C>
const request = {url, method, headers};
const response = {writable: res.writable, headersSent: res.headersSent, headers: res.getHeaders()};
this.logger.critical("Uncaught error", error);
this.logger.debug(error.message, error, {request, response});
this.logger.debug("Uncaught error", error, {request, response});

if (!res.headersSent) {
res.writeHead(500, "Internal Server Error");
Expand Down
2 changes: 1 addition & 1 deletion packages/ws/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"@bunt/util": "^0.19.12",
"@bunt/web": "^0.19.12",
"@types/ws": "^7.4.6",
"ws": "^8.2.0"
"ws": "^8.2.1"
},
"license": "MIT",
"devDependencies": {
Expand Down
10 changes: 5 additions & 5 deletions packages/ws/src/WebSocketServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
unit,
Unit,
} from "@bunt/unit";
import {assert, AsyncState, isDefined, isString, Logger, logger, noop, resolveOrReject} from "@bunt/util";
import {assert, AsyncState, isDefined, isString, Logger, logger, noop, resolveOrReject, toError} from "@bunt/util";
import {RequestMessage, WebServer} from "@bunt/web";
import {IncomingMessage} from "http";
import {Socket} from "net";
Expand Down Expand Up @@ -85,7 +85,7 @@ export class WebSocketServer<C extends Context> implements IDisposable, IRunnabl
webSocket.close(resolveOrReject(resolve, reject));
}));
} catch (error) {
this.logger.error(error.message, error);
this.logger.error("Unexpected error", error);
}
}

Expand Down Expand Up @@ -214,8 +214,8 @@ export class WebSocketServer<C extends Context> implements IDisposable, IRunnabl
this.handle(connection, () => this.#unit.run(route.action, state));
});
} catch (error) {
this.logger.error(error.message, error);
socket.destroy(error);
this.logger.error("Unexpected error", error);
socket.destroy(toError(error, "Unknown"));
}
};

Expand All @@ -224,7 +224,7 @@ export class WebSocketServer<C extends Context> implements IDisposable, IRunnabl
await action();
connection.close(WebSocketCloseReason.NORMAL_CLOSURE);
} catch (error) {
this.logger.error(error.message, error);
this.logger.error("Unexpected error", error);
connection.close(WebSocketCloseReason.INTERNAL_ERROR);
}
}
Expand Down
Loading

0 comments on commit 4d993a1

Please sign in to comment.