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 7 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
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 qrerr
*/
/* globals kjua */

import * as Logger from "/common/modules/Logger/Logger.js";
import * as QrErr from "./qrerr.js";

/**
* 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 QrErr.DataOverflowError() : err;
}
}

/**
Expand Down
12 changes: 12 additions & 0 deletions src/popup/modules/QrLib/qrerr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Errors in QR code generation
*
* @module QrLib/qrerr
ongspxm marked this conversation as resolved.
Show resolved Hide resolved
*/

export class DataOverflowError extends Error {
constructor(...params) {
super(...params);
this.message = 'data overflow error';
}
}
9 changes: 8 additions & 1 deletion src/popup/modules/QrLib/qrgen.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
/* globals qrcodegen */

import * as Logger from "/common/modules/Logger/Logger.js";
import * as QrErr from "./qrerr.js";

const QRC = qrcodegen.QrCode;

Expand Down Expand Up @@ -101,7 +102,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')
? QrErr.DataOverflowError : err;
}

const svgString = qrElem.toSvgString(qrQuietZone);

return getSvgElement(svgString);
Expand Down
14 changes: 12 additions & 2 deletions src/popup/modules/UserInterface.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,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 === "Data too long") {
rugk marked this conversation as resolved.
Show resolved Hide resolved
CommonMessages.showError("errorQrCodeOverflow");
Logger.logError("Data exceeds maximum size:", text.length);
ongspxm marked this conversation as resolved.
Show resolved Hide resolved
} else {
throw e;
ongspxm marked this conversation as resolved.
Show resolved Hide resolved
}
rugk marked this conversation as resolved.
Show resolved Hide resolved
}
}, QR_CODE_REFRESH_TIMEOUT);

/**
Expand Down