badmfck-api-server 4.1.10 → 4.1.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -98,7 +98,7 @@ async function Initializer(services) {
98
98
  }
99
99
  exports.Initializer = Initializer;
100
100
  class APIService extends BaseService_1.BaseService {
101
- version = "4.1.09";
101
+ version = "4.1.12";
102
102
  options;
103
103
  monitor = null;
104
104
  started = new Date();
@@ -550,10 +550,12 @@ class MysqlAdapter {
550
550
  (0, LogService_1.logError)("Connection looks dead, recreating pool 1");
551
551
  this.recreatePool();
552
552
  }
553
- if (trx)
553
+ if (trx && !error.isDuplicateError)
554
554
  this.storeTransactionAsProblem(trx, "error:" + error.errno);
555
- if (request.throwable)
556
- throw { ...DefaultErrors_1.default.DB_ERROR, details: error.message, stack: error };
555
+ if (request.throwable) {
556
+ const base = error.isDuplicateError ? DefaultErrors_1.default.DB_DUPLICATE_ERROR : DefaultErrors_1.default.DB_ERROR;
557
+ throw { ...base, details: error.message, stack: error };
558
+ }
557
559
  return {
558
560
  data: null,
559
561
  error: error,
@@ -799,23 +801,31 @@ class MysqlAdapter {
799
801
  const yyymmdd = new Date().toISOString().substring(0, 10).replaceAll("-", "");
800
802
  const date = new Date();
801
803
  const file = path_1.default.resolve(this.options.transactionFailReportDir, yyymmdd + ".json");
802
- if (this.failReportFileStream) {
803
- if (this.failReportFileStreamName !== file) {
804
+ if (this.failReportFileStream && this.failReportFileStreamName !== file) {
805
+ try {
804
806
  this.failReportFileStream.end();
805
- this.failReportFileStream = null;
806
807
  }
807
- }
808
- if (this.failReportFileStreamName) {
809
- this.failReportFileStream = fs_1.default.createWriteStream(file, { flags: 'a' });
810
- this.failReportFileStreamName = file;
808
+ catch { }
809
+ this.failReportFileStream = null;
810
+ this.failReportFileStreamName = null;
811
811
  }
812
812
  if (this.options.debug)
813
813
  (0, LogService_1.logDB)("Store transaction fail report: ", file, trx.queries, message);
814
- if (!this.failReportFileStream)
815
- this.failReportFileStream = fs_1.default.createWriteStream(file, { flags: 'a' });
814
+ if (!this.failReportFileStream) {
815
+ const stream = fs_1.default.createWriteStream(file, { flags: 'a' });
816
+ stream.on('error', (err) => {
817
+ (0, LogService_1.logCrit)("MysqlAdapter", "Transaction fail report stream error: " + err.message);
818
+ if (this.failReportFileStream === stream) {
819
+ this.failReportFileStream = null;
820
+ this.failReportFileStreamName = null;
821
+ }
822
+ });
823
+ this.failReportFileStream = stream;
824
+ this.failReportFileStreamName = file;
825
+ }
816
826
  this.failReportFileStream.write(JSON.stringify({ queries: trx.queries, time: trx.timestamp, date, message }) + "\n}EOB{\n", err => {
817
827
  if (err)
818
- (0, LogService_1.logCrit)("MysqlAdapter", "Can't write to transaction fail report file");
828
+ (0, LogService_1.logCrit)("MysqlAdapter", "Can't write to transaction fail report file: " + err.message);
819
829
  else
820
830
  (0, LogService_1.logDB)("MysqlAdapter", "Transaction fail report stored");
821
831
  });
@@ -12,6 +12,7 @@ declare class DefaultErrors {
12
12
  static TIMEOUT: IError;
13
13
  static METHOD_NOT_ALLOWED: IError;
14
14
  static DB_ERROR: IError;
15
+ static DB_DUPLICATE_ERROR: IError;
15
16
  static FORBIDDEN: IError;
16
17
  static NOT_FOUND: IError;
17
18
  static INTERNAL_SERVER_ERROR: IError;
@@ -15,6 +15,7 @@ class DefaultErrors {
15
15
  static TIMEOUT = { code: 9, message: "Timeout", httpStatus: 408 };
16
16
  static METHOD_NOT_ALLOWED = { code: 10, message: "Method not allowed", httpStatus: 405 };
17
17
  static DB_ERROR = { code: 11, message: "DB error", httpStatus: 500 };
18
+ static DB_DUPLICATE_ERROR = { code: 28, message: "DB duplicate entry", httpStatus: 409 };
18
19
  static FORBIDDEN = { code: 12, message: "Forbidden", httpStatus: 403 };
19
20
  static NOT_FOUND = { code: 13, message: "Not found", httpStatus: 404 };
20
21
  static INTERNAL_SERVER_ERROR = { code: 14, message: "Internal server error.", httpStatus: 500 };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "badmfck-api-server",
3
- "version": "4.1.10",
3
+ "version": "4.1.12",
4
4
  "description": "Simple API http server based on express",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",