-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DefaultHasOneRepository.get method throws exception if no match found #2472
Comments
@shazha , would you like to submit a patch? |
@dhmlau ,sure, I'll do it later this week. |
As I commented in #2500, I believe the current implementation is intentional and it's just the tsdoc comment that needs fixing. You can read through the discussion in #1658 to better understand our reasoning for throwing EntityNotFound error instead of returning Having said that, I can see how it can be useful to allow Perhaps it's time to resume the work on UncheckedCrudRepository (see #1735) and implement UncheckedHasOneRepository? @raymondfeng @strongloop/loopback-maintainers thoughts? |
At the moment, one can write the following code to handle "not found" case: let report;
try {
report = await this.studyRepository.report(foundStudy.id).get()
} catch (err) {
if (err.code === 'ENTITY_NOT_FOUND') {
report = undefined;
} else {
throw err;
}
} Besides implementing a new repository class, maybe it's enough to add a new method to the existing HasOne repository, e.g. |
It is also possible to write a small helper function to convert ENTITY_NOT_FOUND errors to async function handleNotFound<T>(result: Promise<T>): Promise<T | undefined> {
try {
return await result;
} catch (err) {
if (err.code === 'ENTITY_NOT_FOUND') {
return undefined;
}
throw err;
}
}
// usage
const report = await handleNotFound(
this.studyRepository.report(foundStudy.id).get()
); |
@bajtos , thanks for your detailed comments. I totally understand and agree to throwing But from a developer's point of view, there is a difference between Just my 2 cents, correct me if I'm wrong or misunderstood anything😅 |
@shazha sorry for being late to respond. I see two different consumers of HasOne repository:
Handling of "not found" case affects more than just When implementing a different mode, where |
Closing as appears no longer relevant. Reopen as necessary. |
Description
I have the following two models and HasOne relation for them:
When I'm trying to access
report
with the following code:const report = await this.studyRepository.report(foundStudy.id).get()
if there is no report for the
foundStudy
model, it will throw an exceptionCurrent Behavior
Throws exception for HasOne relation if no match found
Expected Behavior
Returns null or undefined for HasOne relation if no match found.
According to the HasOneRepository interface, it should return null if no match found
However, the DefaultHasOneRepository implementation throws an exception if no match found.
The text was updated successfully, but these errors were encountered: