11const { parseString } = require ( 'xml2js' ) ;
2- const { errors, s3middleware } = require ( 'arsenal' ) ;
2+ const { errors, errorInstances , s3middleware } = require ( 'arsenal' ) ;
33
44const escapeForXml = s3middleware . escapeForXml ;
55const { WebsiteConfiguration } =
@@ -152,7 +152,7 @@ function _validateWebsiteConfigXml(parsingResult) {
152152 if ( ! parsingResult . IndexDocument && ! parsingResult . RedirectAllRequestsTo ) {
153153 errMsg = 'Value for IndexDocument Suffix must be provided if ' +
154154 'RedirectAllRequestsTo is empty' ;
155- return errors . InvalidArgument . customizeDescription ( errMsg ) ;
155+ return errorInstances . InvalidArgument . customizeDescription ( errMsg ) ;
156156 }
157157
158158 if ( parsingResult . RedirectAllRequestsTo ) {
@@ -162,23 +162,23 @@ function _validateWebsiteConfigXml(parsingResult) {
162162 parsingResult . RoutingRules ) {
163163 errMsg = 'RedirectAllRequestsTo cannot be provided in ' +
164164 'conjunction with other Routing Rules.' ;
165- return errors . InvalidRequest . customizeDescription ( errMsg ) ;
165+ return errorInstances . InvalidRequest . customizeDescription ( errMsg ) ;
166166 }
167167 if ( ! xmlContainsElem ( parent , 'HostName' ) ) {
168168 errMsg = 'RedirectAllRequestsTo not well-formed' ;
169- return errors . MalformedXML . customizeDescription ( errMsg ) ;
169+ return errorInstances . MalformedXML . customizeDescription ( errMsg ) ;
170170 }
171171 if ( ! _isValidString ( parent [ 0 ] . HostName [ 0 ] ) ) {
172172 errMsg = 'Valid HostName required in RedirectAllRequestsTo' ;
173- return errors . InvalidRequest . customizeDescription ( errMsg ) ;
173+ return errorInstances . InvalidRequest . customizeDescription ( errMsg ) ;
174174 }
175175 redirectAllObj . hostName = parent [ 0 ] . HostName [ 0 ] ;
176176 if ( xmlContainsElem ( parent , 'Protocol' , { validateParent : false } ) ) {
177177 if ( parent [ 0 ] . Protocol [ 0 ] !== 'http' &&
178178 parent [ 0 ] . Protocol [ 0 ] !== 'https' ) {
179179 errMsg = 'Invalid protocol, protocol can be http or https. ' +
180180 'If not defined, the protocol will be selected automatically.' ;
181- return errors . InvalidRequest . customizeDescription ( errMsg ) ;
181+ return errorInstances . InvalidRequest . customizeDescription ( errMsg ) ;
182182 }
183183 redirectAllObj . protocol = parent [ 0 ] . Protocol [ 0 ] ;
184184 }
@@ -189,11 +189,11 @@ function _validateWebsiteConfigXml(parsingResult) {
189189 const parent = parsingResult . IndexDocument ;
190190 if ( ! xmlContainsElem ( parent , 'Suffix' ) ) {
191191 errMsg = 'IndexDocument is not well-formed' ;
192- return errors . MalformedXML . customizeDescription ( errMsg ) ;
192+ return errorInstances . MalformedXML . customizeDescription ( errMsg ) ;
193193 } else if ( ! _isValidString ( parent [ 0 ] . Suffix [ 0 ] )
194194 || parent [ 0 ] . Suffix [ 0 ] . indexOf ( '/' ) !== - 1 ) {
195195 errMsg = 'IndexDocument Suffix is not well-formed' ;
196- return errors . InvalidArgument . customizeDescription ( errMsg ) ;
196+ return errorInstances . InvalidArgument . customizeDescription ( errMsg ) ;
197197 }
198198 websiteConfig . setIndexDocument ( parent [ 0 ] . Suffix [ 0 ] ) ;
199199 }
@@ -202,11 +202,11 @@ function _validateWebsiteConfigXml(parsingResult) {
202202 const parent = parsingResult . ErrorDocument ;
203203 if ( ! xmlContainsElem ( parent , 'Key' ) ) {
204204 errMsg = 'ErrorDocument is not well-formed' ;
205- return errors . MalformedXML . customizeDescription ( errMsg ) ;
205+ return errorInstances . MalformedXML . customizeDescription ( errMsg ) ;
206206 }
207207 if ( ! _isValidString ( parent [ 0 ] . Key [ 0 ] ) ) {
208208 errMsg = 'ErrorDocument Key is not well-formed' ;
209- return errors . InvalidArgument . customizeDescription ( errMsg ) ;
209+ return errorInstances . InvalidArgument . customizeDescription ( errMsg ) ;
210210 }
211211 websiteConfig . setErrorDocument ( parent [ 0 ] . Key [ 0 ] ) ;
212212 }
@@ -215,15 +215,15 @@ function _validateWebsiteConfigXml(parsingResult) {
215215 const parent = parsingResult . RoutingRules ;
216216 if ( ! xmlContainsElem ( parent , 'RoutingRule' , { isList : true } ) ) {
217217 errMsg = 'RoutingRules is not well-formed' ;
218- return errors . MalformedXML . customizeDescription ( errMsg ) ;
218+ return errorInstances . MalformedXML . customizeDescription ( errMsg ) ;
219219 }
220220 for ( let i = 0 ; i < parent [ 0 ] . RoutingRule . length ; i ++ ) {
221221 const rule = parent [ 0 ] . RoutingRule [ i ] ;
222222 const ruleObj = { redirect : { } } ;
223223 if ( ! _isValidElem ( rule . Redirect ) ) {
224224 errMsg = 'RoutingRule requires Redirect, which is ' +
225225 'missing or not well-formed' ;
226- return errors . MalformedXML . customizeDescription ( errMsg ) ;
226+ return errorInstances . MalformedXML . customizeDescription ( errMsg ) ;
227227 }
228228 // Looks like AWS doesn't actually make this check, but AWS
229229 // documentation specifies at least one of the following elements
@@ -237,7 +237,7 @@ function _validateWebsiteConfigXml(parsingResult) {
237237 errMsg = 'Redirect must contain at least one of ' +
238238 'following: Protocol, HostName, ReplaceKeyPrefixWith, ' +
239239 'ReplaceKeyWith, or HttpRedirectCode element' ;
240- return errors . MalformedXML . customizeDescription ( errMsg ) ;
240+ return errorInstances . MalformedXML . customizeDescription ( errMsg ) ;
241241 }
242242 if ( rule . Redirect [ 0 ] . Protocol ) {
243243 if ( ! _isValidElem ( rule . Redirect [ 0 ] . Protocol ) ||
@@ -246,24 +246,24 @@ function _validateWebsiteConfigXml(parsingResult) {
246246 errMsg = 'Invalid protocol, protocol can be http or ' +
247247 'https. If not defined, the protocol will be selected ' +
248248 'automatically.' ;
249- return errors . InvalidRequest . customizeDescription ( errMsg ) ;
249+ return errorInstances . InvalidRequest . customizeDescription ( errMsg ) ;
250250 }
251251 ruleObj . redirect . protocol = rule . Redirect [ 0 ] . Protocol [ 0 ] ;
252252 }
253253 if ( rule . Redirect [ 0 ] . HttpRedirectCode ) {
254254 errMsg = 'The provided HTTP redirect code is not valid. ' +
255255 'It should be a string containing a number.' ;
256256 if ( ! _isValidElem ( rule . Redirect [ 0 ] . HttpRedirectCode ) ) {
257- return errors . MalformedXML . customizeDescription ( errMsg ) ;
257+ return errorInstances . MalformedXML . customizeDescription ( errMsg ) ;
258258 }
259259 const code = parseInt ( rule . Redirect [ 0 ] . HttpRedirectCode [ 0 ] , 10 ) ;
260260 if ( Number . isNaN ( code ) ) {
261- return errors . MalformedXML . customizeDescription ( errMsg ) ;
261+ return errorInstances . MalformedXML . customizeDescription ( errMsg ) ;
262262 }
263263 if ( ! ( code > 300 && code < 400 ) ) {
264264 errMsg = `The provided HTTP redirect code (${ code } ) is ` +
265265 'not valid. Valid codes are 3XX except 300' ;
266- return errors . InvalidRequest . customizeDescription ( errMsg ) ;
266+ return errorInstances . InvalidRequest . customizeDescription ( errMsg ) ;
267267 }
268268 ruleObj . redirect . httpRedirectCode = code ;
269269 }
@@ -273,7 +273,7 @@ function _validateWebsiteConfigXml(parsingResult) {
273273 if ( elem ) {
274274 if ( ! _isValidElem ( elem ) || ! _isValidString ( elem [ 0 ] ) ) {
275275 errMsg = `Redirect ${ elem } is not well-formed` ;
276- return errors . InvalidArgument
276+ return errorInstances . InvalidArgument
277277 . customizeDescription ( errMsg ) ;
278278 }
279279 ruleObj . redirect [ `${ elemName . charAt ( 0 ) . toLowerCase ( ) } ` +
@@ -286,7 +286,7 @@ function _validateWebsiteConfigXml(parsingResult) {
286286 { validateParent : false , checkForAll : true } ) ) {
287287 errMsg = 'Redirect must not contain both ReplaceKeyWith ' +
288288 'and ReplaceKeyPrefixWith' ;
289- return errors . InvalidRequest . customizeDescription ( errMsg ) ;
289+ return errorInstances . InvalidRequest . customizeDescription ( errMsg ) ;
290290 }
291291 if ( Array . isArray ( rule . Condition ) && rule . Condition . length === 1 ) {
292292 ruleObj . condition = { } ;
@@ -295,14 +295,14 @@ function _validateWebsiteConfigXml(parsingResult) {
295295 errMsg = 'Condition is not well-formed. ' +
296296 'Condition should contain valid KeyPrefixEquals or ' +
297297 'HttpErrorCodeReturnEquals element.' ;
298- return errors . InvalidRequest . customizeDescription ( errMsg ) ;
298+ return errorInstances . InvalidRequest . customizeDescription ( errMsg ) ;
299299 }
300300 if ( rule . Condition [ 0 ] . KeyPrefixEquals ) {
301301 const keyPrefixEquals = rule . Condition [ 0 ] . KeyPrefixEquals ;
302302 if ( ! _isValidElem ( keyPrefixEquals ) ||
303303 ! _isValidString ( keyPrefixEquals [ 0 ] ) ) {
304304 errMsg = 'Condition KeyPrefixEquals is not well-formed' ;
305- return errors . InvalidArgument
305+ return errorInstances . InvalidArgument
306306 . customizeDescription ( errMsg ) ;
307307 }
308308 ruleObj . condition . keyPrefixEquals = keyPrefixEquals [ 0 ] ;
@@ -312,17 +312,17 @@ function _validateWebsiteConfigXml(parsingResult) {
312312 'It should be a string containing a number.' ;
313313 if ( ! _isValidElem ( rule . Condition [ 0 ]
314314 . HttpErrorCodeReturnedEquals ) ) {
315- return errors . MalformedXML . customizeDescription ( errMsg ) ;
315+ return errorInstances . MalformedXML . customizeDescription ( errMsg ) ;
316316 }
317317 const code = parseInt ( rule . Condition [ 0 ]
318318 . HttpErrorCodeReturnedEquals [ 0 ] , 10 ) ;
319319 if ( Number . isNaN ( code ) ) {
320- return errors . MalformedXML . customizeDescription ( errMsg ) ;
320+ return errorInstances . MalformedXML . customizeDescription ( errMsg ) ;
321321 }
322322 if ( ! ( code > 399 && code < 600 ) ) {
323323 errMsg = `The provided HTTP error code (${ code } ) is ` +
324324 'not valid. Valid codes are 4XX or 5XX.' ;
325- return errors . InvalidRequest
325+ return errorInstances . InvalidRequest
326326 . customizeDescription ( errMsg ) ;
327327 }
328328 ruleObj . condition . httpErrorCodeReturnedEquals = code ;
@@ -347,7 +347,7 @@ function parseWebsiteConfigXml(xml, log, cb) {
347347
348348 if ( ! result || ! result . WebsiteConfiguration ) {
349349 const errMsg = 'Invalid website configuration xml' ;
350- return cb ( errors . MalformedXML . customizeDescription ( errMsg ) ) ;
350+ return cb ( errorInstances . MalformedXML . customizeDescription ( errMsg ) ) ;
351351 }
352352
353353 const validationRes =
0 commit comments