@xeplr/base-apis 2.0.5 → 2.0.6

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.
package/README.md CHANGED
@@ -203,7 +203,7 @@ Responses use `@xeplr/utils`' shape: `{ code, message, error, dataArray, updated
203
203
 
204
204
  | error | status |
205
205
  |---|---|
206
- | `ValidationError` | 422 |
206
+ | `ValidationError` | 422. jsonSchema's answers `Validation failed` with `fields`; one a model hook throws with its own `type` (e.g. a licence limit) answers its own `message` |
207
207
  | `UniqueViolationError`, `ForeignKeyViolationError` | 409 |
208
208
  | `NotNullViolationError` (`<column> is required`), `CheckViolationError`, `DataError` | 400 |
209
209
  | `NotFoundError` | 404 |
@@ -7,7 +7,7 @@ var { generateId } = require('@xeplr/utils/lib/helpers');
7
7
  * of a blanket 500. Matched by `err.name` (not instanceof) so it survives two
8
8
  * copies of objection in a linked monorepo. Unknown errors still fall to 500.
9
9
  *
10
- * ValidationError → 422 { fields } (jsonSchema: required/format/…)
10
+ * ValidationError → 422 { fields } (jsonSchema: required/format/…); a hook's own type keeps its message
11
11
  * UniqueViolationError → 409 already exists
12
12
  * NotNullViolationError → 400 <column> is required
13
13
  * ForeignKeyViolationError → 409 referenced record missing / in use
@@ -19,8 +19,12 @@ function sendError(req, res, err) {
19
19
 
20
20
  switch (err && err.name) {
21
21
  case 'ValidationError':
22
+ // A model's own check (a hook throwing ValidationError with a type of
23
+ // its own, e.g. a licence limit) carries a sentence for the person;
24
+ // jsonSchema's (ModelValidation) carries per-field detail instead.
22
25
  return respond(res, HTTP.VALIDATION_ERROR, STATUS.VALIDATION_ERROR, 'validation_error', {
23
- message: 'Validation failed', fields: err.data || {}
26
+ message: err.type && err.type !== 'ModelValidation' && err.message ? err.message : 'Validation failed',
27
+ fields: err.data || {}
24
28
  });
25
29
  case 'UniqueViolationError':
26
30
  return respond(res, HTTP.CONFLICT, STATUS.CONFLICT, 'conflict', {
@@ -66,7 +70,6 @@ function sendError(req, res, err) {
66
70
  */
67
71
  async function save(req, res, hierarchy) {
68
72
  var changeSet = req.body;
69
- console.log('bruh: ', 1);
70
73
  if (!Array.isArray(changeSet) || changeSet.length === 0) {
71
74
  return respond(res, HTTP.BAD_REQUEST, STATUS.BAD_REQUEST, 'bad_request', {
72
75
  message: 'Expected a non-empty changeset array'
@@ -102,7 +105,6 @@ console.log('bruh: ', 1);
102
105
  var ids = await processEntry(changeSet[i], rootDef, trx, null, queryContext);
103
106
  updatedIds = updatedIds.concat(ids);
104
107
  }
105
- console.log('bruh: ', 2);
106
108
 
107
109
  await trx.commit();
108
110
 
@@ -114,7 +116,6 @@ console.log('bruh: ', 2);
114
116
  else console.error('[genericController] afterCommit callback failed:', err.message);
115
117
  }
116
118
  }
117
- console.log('bruh: ', 3);
118
119
 
119
120
  respond(res, HTTP.OK, STATUS.UPDATED, 'updated', { updatedIds: updatedIds });
120
121
  } catch (err) {
@@ -296,4 +297,4 @@ async function list(req, res, Model, graph) {
296
297
  }
297
298
  }
298
299
 
299
- module.exports = { save, deleteByIds, getById, list };
300
+ module.exports = { save, deleteByIds, getById, list, sendError };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xeplr/base-apis",
3
- "version": "2.0.5",
3
+ "version": "2.0.6",
4
4
  "description": "Base API bootstrapper — Express and plain HTTP server factories with middleware injection",
5
5
  "main": "index.js",
6
6
  "bin": {