Release candidate of namespace CRUD
This commit is contained in:
parent
5b82d3b540
commit
8e54879539
11 changed files with 354 additions and 111 deletions
|
@ -2,9 +2,30 @@
|
|||
|
||||
import PropTypes from 'prop-types';
|
||||
|
||||
|
||||
function handleError(that, error) {
|
||||
let errorHandled;
|
||||
if (that.errorHandler) {
|
||||
errorHandled = that.errorHandler(error);
|
||||
}
|
||||
|
||||
if (!errorHandled && that.context.parentErrorHandler) {
|
||||
errorHandled = handleError(that.context.parentErrorHandler, error);
|
||||
}
|
||||
|
||||
if (!errorHandled) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
return errorHandled;
|
||||
}
|
||||
|
||||
function withErrorHandling(target) {
|
||||
const inst = target.prototype;
|
||||
|
||||
if (inst._withErrorHandlingApplied) return;
|
||||
inst._withErrorHandlingApplied = true;
|
||||
|
||||
const contextTypes = target.contextTypes || {};
|
||||
contextTypes.parentErrorHandler = PropTypes.object;
|
||||
target.contextTypes = contextTypes;
|
||||
|
@ -28,24 +49,23 @@ function withErrorHandling(target) {
|
|||
}
|
||||
}
|
||||
|
||||
/* Example of use:
|
||||
this.getFormValuesFromURL(....).catch(error => this.handleError(error));
|
||||
|
||||
It's equivalent to:
|
||||
|
||||
@withAsyncErrorHandler
|
||||
async loadFormValues() {
|
||||
await this.getFormValuesFromURL(...);
|
||||
}
|
||||
*/
|
||||
inst.handleError = function(error) {
|
||||
handleError(this, error);
|
||||
};
|
||||
|
||||
return target;
|
||||
}
|
||||
|
||||
function handleError(that, error) {
|
||||
let errorHandled;
|
||||
if (that.errorHandler) {
|
||||
errorHandled = that.errorHandler(error);
|
||||
}
|
||||
|
||||
if (!errorHandled && that.context.parentErrorHandler) {
|
||||
errorHandled = handleError(that.context.parentErrorHandler, error);
|
||||
}
|
||||
|
||||
if (!errorHandled) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
function withAsyncErrorHandler(target, name, descriptor) {
|
||||
let fn = descriptor.value;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue