Skip to content

Commit 9f70a8a

Browse files
committed
Fix not found error message
1 parent 20aab35 commit 9f70a8a

File tree

5 files changed

+49
-25
lines changed

5 files changed

+49
-25
lines changed

.eslintrc.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ module.exports = {
1919
'@typescript-eslint/explicit-function-return-type': ['off'],
2020
'@typescript-eslint/no-explicit-any': ['off'],
2121
'@typescript-eslint/explicit-member-accessibility': ['off'],
22-
'prefer-const': ['off']
22+
'prefer-const': ['off'],
23+
'prefer-rest-params': ['off']
2324
}
2425
};

src/build-models.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@ export class BaseModel extends Model {
3737
return snakeCaseMappers();
3838
}
3939

40-
static createNotFoundError(): Error {
41-
// const { type, id } = ({} as any).op.record;
42-
const error = new RecordNotFoundException('any', 'any');
40+
static createNotFoundError() {
41+
const context = arguments[0];
42+
const type = (context && context.recordType) || 'unknown type';
43+
const id = (context && context.recordId) || 'unknown id';
44+
const error = new RecordNotFoundException(type, id);
4345
return error as any;
4446
}
4547

src/processor.ts

+35-15
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import { tableize, underscore, foreignKey } from 'inflected';
2828

2929
import { BaseModel, buildModels } from './build-models';
3030
import { migrateModels } from './migrate-models';
31-
import { groupIdentitiesByType } from './utils';
31+
import { groupRecordsByType } from './utils';
3232

3333
export interface ProcessorSettings {
3434
schema: Schema;
@@ -140,7 +140,9 @@ export class Processor {
140140
}
141141

142142
protected async updateRecord(op: UpdateRecordOperation, trx: Transaction) {
143-
const qb = this.queryForType(trx, op.record.type);
143+
const qb = this.queryForType(trx, op.record.type).mergeContext({
144+
recordId: op.record.id
145+
});
144146
const data = this.parseOrbitRecord(op.record);
145147

146148
const model = await qb.upsertGraph(data, {
@@ -153,7 +155,9 @@ export class Processor {
153155

154156
protected async removeRecord(op: RemoveRecordOperation, trx: Transaction) {
155157
const { type, id } = op.record;
156-
const qb = this.queryForType(trx, type);
158+
const qb = this.queryForType(trx, type).mergeContext({
159+
recordId: id
160+
});
157161

158162
const model = (await qb.findById(id)) as BaseModel;
159163
await qb.deleteById(id);
@@ -166,7 +170,9 @@ export class Processor {
166170
trx: Transaction
167171
) {
168172
const { type, id } = op.record;
169-
const qb = this.queryForType(trx, type);
173+
const qb = this.queryForType(trx, type).mergeContext({
174+
recordId: id
175+
});
170176

171177
const model = await qb.patchAndFetchById(id, {
172178
[op.attribute]: op.value
@@ -180,7 +186,9 @@ export class Processor {
180186
trx: Transaction
181187
) {
182188
const { type, id } = op.record;
183-
const qb = this.queryForType(trx, type);
189+
const qb = this.queryForType(trx, type).mergeContext({
190+
recordId: id
191+
});
184192
const relatedId = op.relatedRecord ? op.relatedRecord.id : null;
185193

186194
const model = (await qb.findById(id)) as BaseModel;
@@ -198,7 +206,9 @@ export class Processor {
198206
trx: Transaction
199207
) {
200208
const { type, id } = op.record;
201-
const qb = this.queryForType(trx, type);
209+
const qb = this.queryForType(trx, type).mergeContext({
210+
recordId: id
211+
});
202212
const relatedIds = op.relatedRecords.map(({ id }) => id);
203213

204214
const model = await qb.upsertGraph(
@@ -221,7 +231,9 @@ export class Processor {
221231
trx: Transaction
222232
) {
223233
const { type, id } = op.record;
224-
const qb = this.queryForType(trx, type);
234+
const qb = this.queryForType(trx, type).mergeContext({
235+
recordId: id
236+
});
225237
const relatedId = op.relatedRecord.id;
226238

227239
const model = (await qb.findById(id)) as BaseModel;
@@ -235,7 +247,9 @@ export class Processor {
235247
trx: Transaction
236248
) {
237249
const { type, id } = op.record;
238-
const qb = this.queryForType(trx, type);
250+
const qb = this.queryForType(trx, type).mergeContext({
251+
recordId: id
252+
});
239253

240254
const model = (await qb.findById(id)) as BaseModel;
241255
const relatedId = op.relatedRecord.id;
@@ -249,7 +263,9 @@ export class Processor {
249263

250264
protected async findRecord(expression: FindRecord, trx: Transaction) {
251265
const { id, type } = expression.record;
252-
const qb = this.queryForType(trx, type);
266+
const qb = this.queryForType(trx, type).mergeContext({
267+
recordId: id
268+
});
253269

254270
const model = (await qb.findById(id)) as BaseModel;
255271

@@ -266,12 +282,12 @@ export class Processor {
266282
)) as BaseModel[];
267283
return models.map(model => model.toOrbitRecord());
268284
} else if (records) {
269-
const idsByType = groupIdentitiesByType(records);
285+
const recordsByType = groupRecordsByType(records);
270286
const recordsById: Record<string, OrbitRecord> = {};
271287

272-
for (let type in idsByType) {
288+
for (let type in recordsByType) {
273289
for (let record of await this.queryForType(trx, type, false).findByIds(
274-
idsByType[type]
290+
recordsByType[type]
275291
)) {
276292
recordsById[record.id] = record.toOrbitRecord();
277293
}
@@ -292,7 +308,9 @@ export class Processor {
292308
record: { id, type },
293309
relationship
294310
} = expression;
295-
const qb = this.queryForType(trx, type);
311+
const qb = this.queryForType(trx, type).mergeContext({
312+
recordId: id
313+
});
296314
const { model: relatedType } = this.schema.getRelationship(
297315
type,
298316
relationship
@@ -320,7 +338,9 @@ export class Processor {
320338
relationship
321339
);
322340

323-
let qb = this.queryForType(trx, type);
341+
let qb = this.queryForType(trx, type).mergeContext({
342+
recordId: id
343+
});
324344
const parent = (await qb.findById(id)) as BaseModel;
325345
qb = parent
326346
.$relatedQuery<BaseModel>(relationship, trx)
@@ -342,7 +362,7 @@ export class Processor {
342362

343363
const qb = this.modelForType(type)
344364
.query(trx)
345-
.context({ orbitType: type })
365+
.context({ recordType: type })
346366
.select(fields);
347367

348368
if (throwIfNotFound) {

src/utils.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ export function castAttributeValue(value: unknown, type?: string) {
1717
return value;
1818
}
1919

20-
export function groupIdentitiesByType(identities: RecordIdentity[]) {
21-
const idsByType: Record<string, string[]> = {};
22-
for (let identity of identities) {
23-
idsByType[identity.type] = idsByType[identity.type] || [];
24-
idsByType[identity.type].push(identity.id);
20+
export function groupRecordsByType(records: RecordIdentity[]) {
21+
const recordsByType: Record<string, string[]> = {};
22+
for (let identity of records) {
23+
recordsByType[identity.type] = recordsByType[identity.type] || [];
24+
recordsByType[identity.type].push(identity.id);
2525
}
26-
return idsByType;
26+
return recordsByType;
2727
}

test/sql-source-test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1573,6 +1573,7 @@ QUnit.module('SQLSource', function(hooks) {
15731573
try {
15741574
await source.query(q => q.findRecord({ type: 'planet', id: 'jupiter' }));
15751575
} catch (e) {
1576+
assert.equal(e.message, 'Record not found: planet:jupiter');
15761577
assert.throws(() => {
15771578
throw e;
15781579
}, RecordNotFoundException);

0 commit comments

Comments
 (0)