-
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.
fix: error handling decorator for ember-concurrency tasks (#386)
- Loading branch information
Showing
10 changed files
with
172 additions
and
13 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,87 @@ | ||
import { assert } from "@ember/debug"; | ||
|
||
const catchErrors = (context, args, exception) => { | ||
const [ | ||
{ | ||
routeFor404, | ||
errorMessage = "emeis.general-error", | ||
notFoundErrorMessage = "emeis.not-found", | ||
} = {}, | ||
] = args; | ||
// Transition to route if 404 recieved and routeFor404 is set | ||
if ( | ||
routeFor404 && | ||
exception.isAdapterError && | ||
exception.errors[0].status === "404" | ||
) { | ||
context.notification.danger(context.intl.t(notFoundErrorMessage)); | ||
context.replaceWith(routeFor404); | ||
} else { | ||
console.error(exception); | ||
if ( | ||
!exception.errors || | ||
!exception.errors.map((e) => e.detail).filter(Boolean).length | ||
) { | ||
context.notification.danger(context.intl.t(errorMessage)); | ||
return; | ||
} | ||
exception.errors?.forEach(({ detail }) => { | ||
context.notification.danger(detail); | ||
}); | ||
} | ||
}; | ||
|
||
const validate = (context) => { | ||
assert( | ||
"Inject the `notification` as well as the `intl` service into your route to properly display errors.", | ||
context.notification && context.intl | ||
); | ||
}; | ||
|
||
// make sure that decorator can be called with or without arguments | ||
const makeFlexibleDecorator = (decorateFn, args) => { | ||
if (args.length === 3 && !args[0].routeFor404 && !args[0].errorMessage) { | ||
// We can assume that the decorator was called without options | ||
return decorateFn(...args); | ||
} | ||
|
||
return decorateFn; | ||
}; | ||
|
||
export function handleModelErrors(...decoratorArgs) { | ||
function decorate(target, name, descriptor) { | ||
const originalDescriptor = descriptor.value; | ||
|
||
descriptor.value = function (...args) { | ||
validate(this); | ||
try { | ||
const result = originalDescriptor.apply(this, args); | ||
return result?.then | ||
? result.catch((exception) => | ||
catchErrors(this, decoratorArgs, exception) | ||
) | ||
: result; | ||
} catch (exception) { | ||
catchErrors(this, decoratorArgs, exception); | ||
} | ||
}; | ||
} | ||
|
||
return makeFlexibleDecorator(decorate, decoratorArgs); | ||
} | ||
|
||
export function handleTaskErrors(...decoratorArgs) { | ||
function decorate(target, property, desc) { | ||
const gen = desc.value; | ||
|
||
desc.value = function* (...args) { | ||
validate(this); | ||
try { | ||
return yield* gen.apply(this, args); | ||
} catch (exception) { | ||
catchErrors(this, decoratorArgs, exception); | ||
} | ||
}; | ||
} | ||
return makeFlexibleDecorator(decorate, decoratorArgs); | ||
} |
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
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
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
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
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
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
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
This file was deleted.
Oops, something went wrong.
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,73 @@ | ||
import { restartableTask } from "ember-concurrency"; | ||
import { setupTest } from "ember-qunit"; | ||
import { module, test } from "qunit"; | ||
|
||
import { | ||
handleModelErrors, | ||
handleTaskErrors, | ||
} from "ember-emeis/-private/decorators"; | ||
|
||
module("Unit | decorators", function (hooks) { | ||
setupTest(hooks); | ||
|
||
hooks.beforeEach(function (assert) { | ||
this.TestStub = class TestStub { | ||
intl = { | ||
t: () => { | ||
return "Fehler."; | ||
}, | ||
}; | ||
|
||
notification = { | ||
danger: () => { | ||
assert.step("notify-danger"); | ||
}, | ||
}; | ||
|
||
@handleModelErrors | ||
async fetchModel(shouldThrow) { | ||
if (shouldThrow) { | ||
throw "nope"; | ||
} | ||
return "yeah"; | ||
} | ||
|
||
@restartableTask | ||
@handleTaskErrors | ||
*fetchModelTask(shouldThrow) { | ||
if (shouldThrow) { | ||
throw "nope"; | ||
} | ||
return yield "yeah"; | ||
} | ||
}; | ||
}); | ||
|
||
module("handle-model-errors", function () { | ||
test("doesnt catch successes", async function (assert) { | ||
const instance = new this.TestStub(); | ||
await instance.fetchModel(false); | ||
assert.verifySteps([]); | ||
}); | ||
|
||
test("catches 404 error", async function (assert) { | ||
const instance = new this.TestStub(); | ||
await instance.fetchModel(true); | ||
assert.verifySteps(["notify-danger"]); | ||
}); | ||
}); | ||
|
||
module("handle-task-errors", function () { | ||
test("doesnt catch successes", async function (assert) { | ||
const instance = new this.TestStub(); | ||
await instance.fetchModelTask.perform(false); | ||
assert.verifySteps([]); | ||
}); | ||
|
||
test("catches 404 error", async function (assert) { | ||
const instance = new this.TestStub(); | ||
await instance.fetchModelTask.perform(true); | ||
assert.verifySteps(["notify-danger"]); | ||
}); | ||
}); | ||
}); |