Fixing duplicate sources bug when creating claim#1795
Conversation
452f79e to
a9dbb10
Compare
8910c95 to
8212746
Compare
|
| if (typeof source === "string") { | ||
| const existingSources = await this.sourceService.getSourceByHref(source); | ||
| if (existingSources) { | ||
| this.sourceService.updateTargetId(existingSources._id, claimId) |
There was a problem hiding this comment.
nitpick: consider adding the await keyword in future opportunities
There was a problem hiding this comment.
Awaiting updateTargetId is unnecessary here because its result isn't used, and it's the last operation in this branch, so it doesn't block further processing.
There was a problem hiding this comment.
The try/catch block won't catch possible errors when updating the source target ID if we don't await the response. Following your conclusion, we don't need to await to save documents in the db, since it's the last operation and the result isn't used. However the success or failure status are used in both cases
There was a problem hiding this comment.
Errors will still be caught by the try/catch block, even without await, since promise rejections propagate to the catch.
There was a problem hiding this comment.
Well, not sure about that. could you send me the documentation for that ?
There was a problem hiding this comment.
This is a very simple example where the console "save in the database" occurs even though we got an error in the db transaction. The promise execution starts but does not throw an error inside the try block because it's asynchronous. Any errors will be unhandled unless explicitly caught later.
async function test() {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log('test')
reject(new Error("error"))
}, 2000)
})
}
async function test2() {
try {
test()
console.log("save in the database")
} catch (e) {
console.log("ERROR: ", e.message)
}
}
test2()
// save in the database
// undefined
// VM744:3 test
// VM744:4 Uncaught Error: error
// at <anonymous>:4:15There was a problem hiding this comment.
There was a problem hiding this comment.
After looking into this more deeply, I agree that await should be used for proper error handling. Using await ensures that if the underlying function—like updateTargetId—throws or returns a rejected Promise, the error will be caught in the same try/catch block. However, this only works if the function itself actually propagates errors by awaiting its own database calls. If, for instance, we never await source.save() inside updateTargetId, then a DB error in the save step won’t bubble up, even if we do await updateTargetId in the calling code. So, to fully capture errors, both the function and the caller need to consistently use await.
There was a problem hiding this comment.
Nice, could you provide the documentation that you looked at ? I think it's worth to share with everybody else. Thanks for the discussion, it's always good to have these kind of talks, I almost forgot about the try/catch error handling concept
Fixing duplicate sources bug when creating claim



Description
I debugged the process of creating a claim using both existing and non-existing sources, and I noticed that the conditional meant to determine whether to create or update the sources was always returning true, causing it to always try to create the source even when it already existed in the database, thus generating the error.
Claim created for testing with 3 sources already existing on the site and 3 new ones:




Before creating the claim:
After, adding the target ids to them:
And creating the new ones:
I created a new function to search for the href values inside the source database, and I called this function with the source value received during the claim creation. This way, it returned an array of hrefs that already exist in the database, stored in the
existingSourceconstant. Then, I added a check to see if any value was returned. If any value was found, it would execute the update function, thus updating the source. If it returned an empty array, nothing would be updated, and the source would proceed to theelsewhere it would be created as a new source.Fixes #1796
Type of change
Testing
To test this, you should create claims with existing sources on the site and new sources to test this functionality.
Developer Checklist
General
console.logor related logging is added.Frontend Changes
Backend Changes
Tests
Merge Request Review Checklist