-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(decorators): add error handling for common model usage
- Loading branch information
Showing
2 changed files
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { assert } from "@ember/debug"; | ||
|
||
export default function (...args) { | ||
const [{ routeFor404, errorMessage = "emeis.not-found" } = {}] = args; | ||
|
||
const decorate = function (target, name, descriptor) { | ||
const originalDescriptor = descriptor.value; | ||
|
||
descriptor.value = function (...args) { | ||
assert( | ||
"Inject the `notification` as well as the `intl` service into your route to properly display errors.", | ||
this.notification && this.intl | ||
); | ||
|
||
const catchErrors = (exception) => { | ||
if ( | ||
routeFor404 && | ||
exception.isAdapterError && | ||
exception.errors[0].status === "404" | ||
) { | ||
this.notification.danger(this.intl.t(errorMessage)); | ||
this.replaceWith(routeFor404); | ||
} else { | ||
this.notification.danger(this.intl.t("emeis.general-error")); | ||
} | ||
}; | ||
|
||
try { | ||
const result = originalDescriptor.apply(this, args); | ||
return result.then ? result.catch(catchErrors) : result; | ||
} catch (exception) { | ||
catchErrors(exception); | ||
} | ||
}; | ||
}; | ||
|
||
// We can assume that the decorator was called without options | ||
if (args.length === 3 && !args[0].routeFor404 && !args[0].errorMessage) { | ||
return decorate(...args); | ||
} | ||
|
||
return decorate; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from "ember-emeis/decorators/handle-model-errors"; |