Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display error/warning for too large input #72 #168

Merged
merged 24 commits into from
Mar 22, 2019
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@
"message": "Permission request failed.",
"description": "Error shown when a permission request failed. This indicates a technical error and does NOT mean the user declined the permission."
},
"errorQrCodeOverflow": {
"message": "Cannot generate QR code, the provided text is too much.",
ongspxm marked this conversation as resolved.
Show resolved Hide resolved
"description": "Error shown when QR code cannot be generated due to overflow in input size."
},
"requestDownloadPermissionForQr": {
"message": "To continue to save the QR code, we need the download permission.",
"description": "Shown, when the user is asked to allow the download permission to save the QR code."
Expand Down
12 changes: 12 additions & 0 deletions src/popup/modules/QrLib/QrErrors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Errors in QR code generation
*
* @module QrLib/qrerr
*/

export class DataOverflowError extends Error {
constructor(...params) {
super(...params);
this.message = 'data overflow error';
}
}
10 changes: 9 additions & 1 deletion src/popup/modules/QrLib/kjua.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
*
* @module QrLib/kjua
* @requires /common/modules/Logger
* @requires QrErrors
*/
/* globals kjua */

import * as Logger from "/common/modules/Logger/Logger.js";
import * as QrError from "./QrErrors.js";
ongspxm marked this conversation as resolved.
Show resolved Hide resolved

/**
* The type of QR code this library generates.
Expand Down Expand Up @@ -112,7 +114,13 @@ export function set(tag, value) {
*/
export function getQr() {
Logger.logInfo("generated new qr kjua code", kjuaOptions);
return kjua(kjuaOptions);

try {
return kjua(kjuaOptions);
} catch (err) {
throw err.message.startsWith("code length overflow.")
? new QrError.DataOverflowError() : err;
ongspxm marked this conversation as resolved.
Show resolved Hide resolved
}
}

/**
Expand Down
10 changes: 9 additions & 1 deletion src/popup/modules/QrLib/qrgen.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
*
* @module QrLib/qrgen
* @requires /common/modules/Logger
* @requires QrErrors
*/
/* globals qrcodegen */

import * as Logger from "/common/modules/Logger/Logger.js";
import * as QrError from "./QrErrors.js";

const QRC = qrcodegen.QrCode;

Expand Down Expand Up @@ -101,7 +103,13 @@ export function set(tag, value) {
export function getQr() {
Logger.logInfo("generated new QrGen qr code");

const qrElem = QRC.encodeText(qrText, qrErrorCorrection);
try {
const qrElem = QRC.encodeText(qrText, qrErrorCorrection);
rugk marked this conversation as resolved.
Show resolved Hide resolved
} catch (err) {
throw (err === "Data too long")
? QrError.DataOverflowError : err;
}

const svgString = qrElem.toSvgString(qrQuietZone);

return getSvgElement(svgString);
Expand Down
16 changes: 14 additions & 2 deletions src/popup/modules/UserInterface.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* @requires /common/modules/Logger
* @requires /common/modules/AddonSettings
* @requires /common/modules/MessageHandler
* @requires ./QrLib/QrErrors
* @requires ./QrCreator
*/
// lodash
Expand All @@ -20,6 +21,7 @@ import * as Logger from "/common/modules/Logger/Logger.js";
import * as AddonSettings from "/common/modules/AddonSettings/AddonSettings.js";
import * as CommonMessages from "/common/modules/MessageHandler/CommonMessages.js";

import * as QrError from "./QrLib/QrErrors.js";
import * as QrCreator from "./QrCreator.js";
import {createMenu} from "/common/modules/ContextMenu.js";

Expand Down Expand Up @@ -109,8 +111,18 @@ const refreshQrCode = throttle(() => {
CommonMessages.hideError();
}

QrCreator.setTextInternal(text);
QrCreator.generate();
try {
QrCreator.setTextInternal(text);
QrCreator.generate();
} catch (e) {
// Error thrown from qrcodegen & kjua wrapper when code too long
ongspxm marked this conversation as resolved.
Show resolved Hide resolved
if (e instanceof QrError.DataOverflowError) {
CommonMessages.showError("errorQrCodeOverflow");
Logger.logError("Data exceeds maximum size:", text.length);
ongspxm marked this conversation as resolved.
Show resolved Hide resolved
} else {
throw e;
}
rugk marked this conversation as resolved.
Show resolved Hide resolved
}
}, QR_CODE_REFRESH_TIMEOUT);

/**
Expand Down