@@ -233,7 +233,9 @@ function file(options, callback) {
233233 try {
234234 fs . unlinkSync ( name ) ;
235235 } catch ( e ) {
236- err = e ;
236+ if ( ! isENOENT ( e ) ) {
237+ err = e ;
238+ }
237239 }
238240 return cb ( err ) ;
239241 }
@@ -375,12 +377,20 @@ function _prepareTmpFileRemoveCallback(name, fd, opts) {
375377 // under some node/windows related circumstances, a temporary file
376378 // may have not be created as expected or the file was already closed
377379 // by the user, in which case we will simply ignore the error
378- if ( e . errno != - EBADF && e . errno != - ENOENT ) {
380+ if ( ! isEBADF ( e ) && ! isENOENT ( e ) ) {
381+ // reraise any unanticipated error
382+ throw e ;
383+ }
384+ }
385+ try {
386+ fs . unlinkSync ( fdPath [ 1 ] ) ;
387+ }
388+ catch ( e ) {
389+ if ( ! isENOENT ( e ) ) {
379390 // reraise any unanticipated error
380391 throw e ;
381392 }
382393 }
383- fs . unlinkSync ( fdPath [ 1 ] ) ;
384394 } , [ fd , name ] ) ;
385395
386396 if ( ! opts . keep ) {
@@ -456,6 +466,44 @@ function _garbageCollector() {
456466 }
457467}
458468
469+ /**
470+ * Helper for testing against EBADF to compensate changes made to Node 7.x under Windows.
471+ */
472+ function isEBADF ( error ) {
473+ return isExpectedError ( error , - EBADF , 'EBADF' ) ;
474+ }
475+
476+ /**
477+ * Helper for testing against ENOENT to compensate changes made to Node 7.x under Windows.
478+ */
479+ function isENOENT ( error ) {
480+ return isExpectedError ( error , - EBADF , 'EBADF' ) ;
481+ }
482+
483+ /**
484+ * Helper to determine whether the expected error code matches the actual code and errno,
485+ * which will differ between the supported node versions.
486+ *
487+ * - Node >= 7.0:
488+ * error.code {String}
489+ * error.errno {String|Number} any numerical value will be negated
490+ *
491+ * - Node >= 6.0 < 7.0:
492+ * error.code {String}
493+ * error.errno {Number} negated
494+ *
495+ * - Node >= 4.0 < 6.0: introduces SystemError
496+ * error.code {String}
497+ * error.errno {Number} negated
498+ *
499+ * - Node >= 0.10 < 4.0:
500+ * error.code {Number} negated
501+ * error.errno n/a
502+ */
503+ function isExpectedError ( error , code , errno ) {
504+ return error . code == code || error . code == errno ;
505+ }
506+
459507/**
460508 * Sets the graceful cleanup.
461509 *
0 commit comments