Skip to content

Commit

Permalink
PB-984: avoid duplicate errors
Browse files Browse the repository at this point in the history
Issue : sometimes, errors and warning might get added multiple times, and as they are new object instances, the set had issue ensuring unicity

Fix : we filter the errors and warnings received, ensuring we only add messages with a different message AND different parameters.

PB-984: putting equality check in class for easier readability
  • Loading branch information
ltkum committed Oct 8, 2024
1 parent f477497 commit ccd9edc
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/store/modules/ui.store.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,12 +389,21 @@ export default {
setShowDisclaimer({ commit }, { showDisclaimer, dispatcher }) {
commit('setShowDisclaimer', { showDisclaimer, dispatcher })
},
addErrors({ commit }, { errors, dispatcher }) {
addErrors({ commit, state }, { errors, dispatcher }) {
if (
errors instanceof Array &&
errors.filter((error) => error instanceof ErrorMessage).length === errors.length
) {
commit('addErrors', { errors, dispatcher })
errors = errors.filter(
(error) =>
// we only add the errors that are not existing within the store
state.errors.filter((otherError) => {
error.isEquals(otherError)
}).length === 0
)
if (errors.length > 0) {
commit('addErrors', { errors, dispatcher })
}
} else {
throw new Error(
`Error ${errors} dispatched by ${dispatcher} is not of type ErrorMessage, or not an Array of ErrorMessages`
Expand All @@ -412,13 +421,22 @@ export default {
commit('removeError', { error, dispatcher })
}
},
addWarnings({ commit }, { warnings, dispatcher }) {
addWarnings({ commit, state }, { warnings, dispatcher }) {
if (
warnings instanceof Array &&
warnings.filter((warning) => warning instanceof WarningMessage).length ===
warnings.length
) {
commit('addWarnings', { warnings, dispatcher })
warnings = warnings.filter(
(warning) =>
// we only add the warnings that are not existing within the store
state.warnings.filter((otherWarning) => {
warning.isEquals(otherWarning)
}).length === 0
)
if (warnings.length > 0) {
commit('addWarnings', { warnings, dispatcher })
}
} else {
throw new Error(
`Warning ${warnings} dispatched by ${dispatcher} is not of type WarningMessage, or not an Array of WarningMessages`
Expand Down
8 changes: 8 additions & 0 deletions src/utils/ErrorMessage.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,12 @@ export default class ErrorMessage {
this.msg = msg
this.params = params
}

isEquals(object) {
return (
object instanceof ErrorMessage &&
object.msg === this.msg &&
object.params === this.params
)
}
}
8 changes: 8 additions & 0 deletions src/utils/WarningMessage.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,12 @@ export default class WarningMessage {
this.msg = msg
this.params = params
}

isEquals(object) {
return (
object instanceof WarningMessage &&
object.msg === this.msg &&
object.params === this.params
)
}
}

0 comments on commit ccd9edc

Please sign in to comment.