@chevre/domain 23.0.0-alpha.18 → 23.0.0-alpha.19

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.
@@ -0,0 +1,37 @@
1
+ // tslint:disable:no-implicit-dependencies no-console
2
+ import * as mongoose from 'mongoose';
3
+ import { chevre } from '../../../../lib/index';
4
+
5
+ const project = { id: String(process.env.PROJECT_ID) };
6
+
7
+ async function main() {
8
+ try {
9
+ await mongoose.connect(<string>process.env.MONGOLAB_URI, { autoIndex: false });
10
+
11
+ const paymentServiceRepo = await chevre.repository.PaymentService.createInstance(mongoose.connection);
12
+
13
+ const limit = 10;
14
+ const page = 1;
15
+ const docs = await paymentServiceRepo.projectFields(
16
+ {
17
+ limit,
18
+ page,
19
+ project: { id: { $eq: project.id } },
20
+ availableChannel: { id: { $eq: 'xxx' } },
21
+ id: { $eq: 'xxx' }
22
+ },
23
+ ['availableChannel', 'productID']
24
+ );
25
+ // tslint:disable-next-line:no-null-keyword
26
+ console.dir(docs, { depth: null });
27
+ console.log(docs.length, 'docs found');
28
+ } catch (error) {
29
+ throw await chevre.errorHandler.handleMongoError(error);
30
+ }
31
+ }
32
+
33
+ main()
34
+ .then(() => {
35
+ console.log('success!');
36
+ })
37
+ .catch(console.error);
@@ -24,6 +24,7 @@ exports.handleAWSError = handleAWSError;
24
24
  const http_status_1 = require("http-status");
25
25
  const factory_1 = require("./factory");
26
26
  let mongo;
27
+ let mongooseError;
27
28
  /**
28
29
  * https://www.mongodb.com/ja-jp/docs/manual/reference/error-codes/
29
30
  */
@@ -44,9 +45,13 @@ function isMongoError(error) {
44
45
  }
45
46
  function handleMongoError(error) {
46
47
  return __awaiter(this, void 0, void 0, function* () {
48
+ var _a;
47
49
  if (mongo === undefined) {
48
50
  mongo = (yield Promise.resolve().then(() => require('mongoose'))).mongo;
49
51
  }
52
+ if (mongooseError === undefined) {
53
+ mongooseError = (yield Promise.resolve().then(() => require('mongoose'))).Error;
54
+ }
50
55
  let handledError = error;
51
56
  if (handledError instanceof mongo.MongoError || handledError instanceof mongo.MongoServerError) {
52
57
  switch (handledError.code) {
@@ -66,6 +71,11 @@ function handleMongoError(error) {
66
71
  if (mongo.BSON.BSONError.isBSONError(handledError)) {
67
72
  handledError = new factory_1.errors.Argument('', handledError.message);
68
73
  }
74
+ // handle CastError(2025-10-27~)
75
+ // CastError: Cast to ObjectId failed for value "xxx" (type string) at path "_id" for model "xxxxxxxxxxxxx"
76
+ if (handledError instanceof mongooseError.CastError) {
77
+ handledError = new factory_1.errors.Argument(`${(_a = handledError.model) === null || _a === void 0 ? void 0 : _a.modelName}.${handledError.path}`, (handledError.reason instanceof Error) ? handledError.reason.message : handledError.message);
78
+ }
69
79
  return handledError;
70
80
  });
71
81
  }
package/package.json CHANGED
@@ -115,5 +115,5 @@
115
115
  "postversion": "git push origin --tags",
116
116
  "prepublishOnly": "npm run clean && npm run build && npm test && npm run doc"
117
117
  },
118
- "version": "23.0.0-alpha.18"
118
+ "version": "23.0.0-alpha.19"
119
119
  }
@@ -1,32 +0,0 @@
1
- // tslint:disable:no-implicit-dependencies no-console
2
- import * as mongoose from 'mongoose';
3
- import { chevre } from '../../../lib/index';
4
-
5
- const project = { id: String(process.env.PROJECT_ID) };
6
-
7
- async function main() {
8
- await mongoose.connect(<string>process.env.MONGOLAB_URI, { autoIndex: false });
9
-
10
- const paymentServiceRepo = await chevre.repository.PaymentService.createInstance(mongoose.connection);
11
-
12
- const limit = 10;
13
- const page = 1;
14
- const docs = await paymentServiceRepo.projectFields(
15
- {
16
- limit,
17
- page,
18
- project: { id: { $eq: project.id } },
19
- availableChannel: { id: { $eq: 'xxx' } }
20
- },
21
- ['availableChannel', 'productID']
22
- );
23
- // tslint:disable-next-line:no-null-keyword
24
- console.dir(docs, { depth: null });
25
- console.log(docs.length, 'docs found');
26
- }
27
-
28
- main()
29
- .then(() => {
30
- console.log('success!');
31
- })
32
- .catch(console.error);