@@ -52,6 +52,13 @@ angular.module('import').directive('connectionImportErrors', [
5252 */
5353 patchFailure : '=' ,
5454
55+ /**
56+ * The DirectoryPatch list sent to patchConnections. It is used to
57+ * map API errors back to import file rows.
58+ *
59+ * @type {DirectoryPatch[] }
60+ */
61+ patches : '='
5562 }
5663 } ;
5764
@@ -114,7 +121,7 @@ angular.module('import').directive('connectionImportErrors', [
114121
115122 /**
116123 * Generate a ImportConnectionError representing any errors associated
117- * with the row at the given index within the given parse result.
124+ * with the row at the given index within the given patch result.
118125 *
119126 * @param {ParseResult } parseResult
120127 * The result of parsing the connection import file.
@@ -131,37 +138,36 @@ angular.module('import').directive('connectionImportErrors', [
131138 * The connection error object associated with the given row in the
132139 * given parse result.
133140 */
134- const generateConnectionError = ( parseResult , index , row ) => {
141+ const generatePatchConnectionError = ( parseResult , patches , index , row ) => {
135142
136143 // Get the patch associated with the current row
137- const patch = parseResult . patches [ index ] ;
144+ const patch = patches [ index ] ;
138145
139146 // The value of a patch is just the Connection object
140- const connection = patch . value ;
147+ const connectionObject = patch . value ;
141148
142149 return new ImportConnectionError ( {
143150
144151 // Add 1 to the provided row to get the position in the file
145152 rowNumber : row + 1 ,
146153
147154 // Basic connection information - name, group, and protocol.
148- name : connection . name ,
155+ name : connectionObject . name ,
149156 group : parseResult . groupPaths [ index ] ,
150- protocol : connection . protocol ,
157+ protocol : connectionObject . protocol ,
151158
152159 // The human-readable error messages
153- errors : new DisplayErrorList (
154- [ ...( parseResult . errors [ index ] || [ ] ) ] )
160+ errors : new DisplayErrorList ( [ ] )
155161 } ) ;
156162 } ;
157163
158164 // If a new connection patch failure is seen, update the display list
159165 $scope . $watch ( 'patchFailure' , function patchFailureChanged ( patchFailure ) {
160166
161- const { parseResult } = $scope ;
167+ const { parseResult, patches } = $scope ;
162168
163169 // Do not attempt to process anything before the data has loaded
164- if ( ! patchFailure || ! parseResult )
170+ if ( ! patchFailure || ! parseResult || ! patches )
165171 return ;
166172
167173 // All promises from all translation requests. The scope will not be
@@ -183,7 +189,7 @@ angular.module('import').directive('connectionImportErrors', [
183189
184190 // Set up the list of connection errors based on the existing parse
185191 // result, with error messages fetched from the patch failure
186- const connectionErrors = parseResult . patches . reduce (
192+ const connectionErrors = patches . reduce (
187193 ( errors , patch , index ) => {
188194
189195 // Do not process display REMOVE patches - they are always
@@ -204,8 +210,8 @@ angular.module('import').directive('connectionImportErrors', [
204210 }
205211
206212 // Generate a connection error for display
207- const connectionError = generateConnectionError (
208- parseResult , index , row ++ ) ;
213+ const connectionError = generatePatchConnectionError (
214+ parseResult , patches , index , row ++ ) ;
209215
210216 // Add the error associated with the previous REMOVE patch, if
211217 // any, to the error associated with the current patch, if any
@@ -238,6 +244,45 @@ angular.module('import').directive('connectionImportErrors', [
238244
239245 } ) ;
240246
247+ /**
248+ * Generate a ImportConnectionError representing any errors associated
249+ * with the row at the given index within the given parse result.
250+ *
251+ * @param {ParseResult } parseResult
252+ * The result of parsing the connection import file.
253+ *
254+ * @param {Integer } index
255+ * The current row within the patches array, 0-indexed.
256+ *
257+ * @param {Integer } row
258+ * The current row within the original connection, 0-indexed.
259+ * If any REMOVE patches are present, this may be greater than
260+ * the index.
261+ *
262+ * @returns {ImportConnectionError }
263+ * The connection error object associated with the given row in the
264+ * given parse result.
265+ */
266+ const generateParseConnectionError = ( parseResult , index , row ) => {
267+
268+ // Get the connection details
269+ const connectionObject = parseResult . connectionObjects [ index ] ;
270+
271+ return new ImportConnectionError ( {
272+
273+ // Physical position in the file
274+ rowNumber : row + 1 ,
275+
276+ // Basic connection information - name, group, and protocol.
277+ name : connectionObject . name ,
278+ group : connectionObject . group ,
279+ protocol : connectionObject . protocol ,
280+
281+ // Pull parse-time errors from the ImportConnection object
282+ errors : new DisplayErrorList ( [ ...( connectionObject . errors || [ ] ) ] )
283+ } ) ;
284+ } ;
285+
241286 // If a new parse result with errors is seen, update the display list
242287 $scope . $watch ( 'parseResult' , function parseResultChanged ( parseResult ) {
243288
@@ -254,62 +299,47 @@ angular.module('import').directive('connectionImportErrors', [
254299 // entirely - if set, they will be from the previous file and no
255300 // longer relevant.
256301
257- // The row number for display. Unlike the index, this number will
258- // skip any REMOVE patches. In other words, this is the index of
259- // connections within the original import file.
260- let row = 0 ;
261-
262302 // Set up the list of connection errors based on the updated parse
263- // result
264- const connectionErrors = parseResult . patches . reduce (
265- ( errors , patch , index ) => {
303+ // result using connectionObjects.
304+ const connectionErrors = parseResult . connectionObjects . map (
305+ ( connectionObject , index ) => {
266306
267- // Do not process display REMOVE patches - they are always
268- // followed by ADD patches containing the actual content
269- // (and errors, if any)
270- if ( patch . op === DirectoryPatch . Operation . REMOVE )
271- return errors ;
272-
273- // Generate a connection error for display
274- const connectionError = generateConnectionError (
275- parseResult , index , row ++ ) ;
307+ // Generate a connection error for display.
308+ // In this new architecture, index and row are identical.
309+ const connectionError = generateParseConnectionError (
310+ parseResult , index , index ) ;
276311
277- // Go through the errors and check if any are translateable
278- connectionError . errors . getArray ( ) . forEach (
279- ( error , errorIndex ) => {
312+ // Go through the errors and check if any are translateable.
313+ // We access the underlying array from the DisplayErrorList.
314+ connectionError . errors . getArray ( ) . forEach ( ( error , errorIndex ) => {
280315
281316 // If this error is a ParseError, it can be translated.
282- // NOTE: Generally one would translate error messages in the
283- // template, but in this case, the connection errors need to
284- // be raw strings in order to enable sorting and filtering.
285- if ( error instanceof ParseError )
317+ if ( error instanceof ParseError ) {
286318
287319 // Fetch the translation and update it when it's ready
288320 translationPromises . push ( $translate (
289- error . key , error . variables )
290- . then ( translatedError => {
291- connectionError . errors . getArray ( ) [ errorIndex ] = translatedError ;
292- } ) ) ;
321+ error . key , error . variables ) . then ( translatedError => {
322+ connectionError . errors . getArray ( ) [ errorIndex ] = translatedError ;
323+ } ) ) ;
324+ }
293325
294326 // If the error is not a known translatable type, add the
295327 // message directly to the error array
296- else
297- connectionError . errors . getArray ( ) [ errorIndex ] = (
298- error . message ? error . message : error ) ;
328+ else {
329+ connectionError . errors . getArray ( ) [ errorIndex ] = (
330+ error . message ? error . message : error ) ;
331+ }
299332
300333 } ) ;
301334
302- errors . push ( connectionError ) ;
303- return errors ;
304-
305- } , [ ] ) ;
335+ return connectionError ;
336+ } ) ;
306337
307338 // Once all the translations have been completed, update the
308339 // connectionErrors all in one go, to ensure no excessive reloading
309340 $q . all ( translationPromises ) . then ( ( ) => {
310341 $scope . connectionErrors = connectionErrors ;
311342 } ) ;
312-
313343 } ) ;
314344
315345 } ] ;
0 commit comments