forked from Breeze/breeze.js.labs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
breeze.saveErrorExtensions.js
145 lines (131 loc) · 4.41 KB
/
breeze.saveErrorExtensions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* Extend breeze with saveErrorMessageService,
* a utility service for processing errors returned from a save.
*
* Depends on Breeze which it patches
*
* `getErrorMessage is its primary method (see example).
*
* The default implementation extracts validation error messages and
* arranges for server validation errors to be removed from an entity
* the next time this entity changes in any way.
*
* The service API includes implementation methods that can be overriden (replaced)
* to change the behavior just described
*
* This extension is not prescriptive!
* It is only one approach to save error message handling.
* Use it for inspiration.
*
* Copyright 2013 IdeaBlade, Inc. All Rights Reserved.
* Licensed under the MIT License
* http://opensource.org/licenses/mit-license.php
* Author: Ward Bell
*
* v.1.0.3
*
* Install:
* 1) include this script after breeze script
* 2) use it in your save failure handler
*
* Example:
* return manager.saveChanges().then(saveSucceeded, saveFailed);
*
* function saveFailed(error) {
* var msg = 'Save failed: ' + breeze.saveErrorMessageService.getErrorMessage(error);
* error.message = msg;
* logError(msg, error);
* throw error;
* }
*
*/
(function (definition, window) {
if (window.breeze) {
definition(window.breeze);
} else if (typeof require === "function" && typeof exports === "object" && typeof module === "object") {
// CommonJS or Node
var b = require('breeze');
definition(b);
} else if (typeof define === "function" && define["amd"] && !window.breeze) {
// Requirejs / AMD
define(['breeze'], definition);
} else {
throw new Error("Can't find breeze");
}
}(function (breeze) {
'use strict';
var service = {
// The main service function
getErrorMessage: getErrorMessage,
// Replaceable implementation methods
getEntityName: getEntityName,
getMessageFromEntityError: getMessageFromEntityError,
reviewServerErrors: reviewServerErrors
}
breeze.saveErrorMessageService = service;
// The main service function
function getErrorMessage(error) {
var msg = error.message;
var entityErrors = error.entityErrors;
if (entityErrors && entityErrors.length) {
service.reviewServerErrors(entityErrors);
return getValidationMessages(entityErrors);
}
return msg;
}
function getValidationMessages(entityErrors) {
var isServerError = entityErrors[0].isServerError; // if the first is, they all are
try {
return entityErrors.map(service.getMessageFromEntityError).join('; <br/>');
} catch (e) {
/* eat it for now */
return (isServerError ? 'server' : 'client') + ' validation error';
}
}
// default implementation of service.getMessageFromEntityError
function getMessageFromEntityError(entityError){
var entity = entityError.entity;
if (entity) {
var name = service.getEntityName(entity);
}
name = name ? name += ' - ' : '';
return name + '\'' + entityError.errorMessage + '\'';
}
// default implementation of service.getEntityName
function getEntityName(entity) {
var key = entity.entityAspect.getKey();
var name = key.entityType.shortName;
var id = key.values.join(',');
return name + ' (' + id + ')';
}
// default implementation of service.reviewServerErrors
function reviewServerErrors(entityErrors){
var entitiesWithServerErrors = [];
entityErrors.forEach(function(entityError){
var entity = entityError.isServerError && entityError.entity;
if (entity && entitiesWithServerErrors.indexOf(entity) === -1) {
entitiesWithServerErrors.push(entity);
clearServerErrorsOnNextChange(entity);
}
})
}
function clearServerErrorsOnNextChange(badEntity) {
if (badEntity.entityAspect.entityState.isDetached()) { return; }
// implemented as a one-time, propertyChanged eventhandler that
// clears the server validation errors if anything happens to this entity
(function(entity) {
var manager = entity.entityAspect.entityManager;
var subKey = manager.entityChanged.subscribe(function(changeArgs) {
if (changeArgs.entity === entity) {
manager.entityChanged.unsubscribe(subKey);
var aspect = entity.entityAspect;
aspect.getValidationErrors().forEach(function(err) {
if (err.isServerError) {
aspect.removeValidationError(err);
}
});
}
});
})(badEntity);
}
}, this));