Merge pull request #5149 from betagouv/file-upload-read-message

Upload des fichiers : seuls les messages d'erreurs inattendus sont enregistrés dans Sentry
This commit is contained in:
Pierre de La Morinerie 2020-05-14 14:17:19 +02:00 committed by GitHub
commit e2d72724d4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 38 deletions

View file

@ -1,6 +1,9 @@
import Uploader from '../../shared/activestorage/uploader'; import Uploader from '../../shared/activestorage/uploader';
import { show, hide, toggle } from '@utils'; import { show, hide, toggle } from '@utils';
import { FAILURE_CONNECTIVITY } from '../../shared/activestorage/file-upload-error'; import {
ERROR_CODE_READ,
FAILURE_CONNECTIVITY
} from '../../shared/activestorage/file-upload-error';
// Given a file input in a champ with a selected file, upload a file, // Given a file input in a champ with a selected file, upload a file,
// then attach it to the dossier. // then attach it to the dossier.
@ -68,6 +71,12 @@ export default class AutoUploadController {
description: 'Vérifiez votre connexion à Internet, puis ré-essayez.', description: 'Vérifiez votre connexion à Internet, puis ré-essayez.',
retry: true retry: true
}; };
} else if (error.code == ERROR_CODE_READ) {
return {
title: 'Nous narrivons pas à lire ce fichier sur votre appareil.',
description: 'Essayez à nouveau, ou sélectionnez un autre fichier.',
retry: false
};
} else { } else {
return { return {
title: 'Le fichier na pas pu être envoyé.', title: 'Le fichier na pas pu être envoyé.',

View file

@ -1,12 +1,9 @@
import Rails from '@rails/ujs'; import Rails from '@rails/ujs';
import AutoUploadController from './auto-upload-controller.js'; import AutoUploadController from './auto-upload-controller.js';
import { fire } from '@utils'; import {
import { FAILURE_CONNECTIVITY } from '../../shared/activestorage/file-upload-error'; FAILURE_CLIENT,
ERROR_CODE_READ
// } from '../../shared/activestorage/file-upload-error';
// DEBUG
//
const originalImpl = FileReader.prototype.addEventListener;
// Manage multiple concurrent uploads. // Manage multiple concurrent uploads.
// //
@ -24,10 +21,11 @@ export default class AutoUploadsControllers {
try { try {
let controller = new AutoUploadController(input, file); let controller = new AutoUploadController(input, file);
await controller.start(); await controller.start();
} catch (error) { } catch (err) {
// Report errors to Sentry (except connectivity issues) // Report unexpected client errors to Sentry.
if (error.failureReason != FAILURE_CONNECTIVITY) { // (But ignore usual client errors, or errors we can monitor better on the server side.)
throw error; if (err.failureReason == FAILURE_CLIENT && err.code != ERROR_CODE_READ) {
throw err;
} }
} finally { } finally {
this._decrementInFlightUploads(form); this._decrementInFlightUploads(form);
@ -42,25 +40,6 @@ export default class AutoUploadsControllers {
.querySelectorAll('button[type=submit]') .querySelectorAll('button[type=submit]')
.forEach((submitButton) => Rails.disableElement(submitButton)); .forEach((submitButton) => Rails.disableElement(submitButton));
} }
//
// DEBUG: hook into FileReader onload event
//
if (FileReader.prototype.addEventListener === originalImpl) {
FileReader.prototype.addEventListener = function () {
// When DirectUploads attempts to add an event listener for "error",
// also insert a custom event listener of our that will report errors to Sentry.
if (arguments[0] == 'error') {
let handler = (event) => {
let message = `FileReader ${event.target.error.name}: ${event.target.error.message}`;
fire(document, 'sentry:capture-exception', new Error(message));
};
originalImpl.apply(this, ['error', handler]);
}
// Add the originally requested event listener
return originalImpl.apply(this, arguments);
};
}
} }
_decrementInFlightUploads(form) { _decrementInFlightUploads(form) {
@ -73,12 +52,5 @@ export default class AutoUploadsControllers {
.querySelectorAll('button[type=submit]') .querySelectorAll('button[type=submit]')
.forEach((submitButton) => Rails.enableElement(submitButton)); .forEach((submitButton) => Rails.enableElement(submitButton));
} }
//
// DEBUG: remove the FileReader hook we set before.
//
if (this.inFlightUploadsCount == 0) {
FileReader.prototype.addEventListener = originalImpl;
}
} }
} }