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 all 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 long.",
"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
2 changes: 2 additions & 0 deletions src/popup/lib/kjua-0.2.0.min.js

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions src/popup/lib/kjua.min.js

This file was deleted.

13 changes: 13 additions & 0 deletions src/popup/modules/QrLib/QrError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Errors in QR code generation
*
* @module QrLib/QrError
*/

export class DataOverflowError extends Error {
constructor(message, ...params) {
super(
message || "The QR code was given too much data.",
...params);
}
}
12 changes: 10 additions & 2 deletions src/popup/modules/QrLib/kjua.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
* Creates and modifies a QR code with the QR code library "kjua".
*
* @module QrLib/kjua
* @requires /common/modules/Logger
* @requires QrError
*/
/* globals kjua */

import * as QrError from "./QrError.js";

/**
* The type of QR code this library generates.
*
Expand Down Expand Up @@ -110,7 +112,13 @@ export function set(tag, value) {
*/
export function getQr() {
console.info("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;
}
}

/**
Expand Down
15 changes: 10 additions & 5 deletions src/popup/modules/QrLib/qrgen.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
* Creates and modifies a QR code with the QR code library "kjua".
*
* @module QrLib/qrgen
* @requires /common/modules/Logger
* @requires QrError
*/
/* globals qrcodegen */

import * as QrError from "./QrError.js";
const QRC = qrcodegen.QrCode;

let qrQuietZone;
Expand Down Expand Up @@ -99,8 +100,12 @@ export function set(tag, value) {
export function getQr() {
console.info("generated new QrGen qr code");

const qrElem = QRC.encodeText(qrText, qrErrorCorrection);
const svgString = qrElem.toSvgString(qrQuietZone);

return getSvgElement(svgString);
try {
const qrElem = QRC.encodeText(qrText, qrErrorCorrection);
rugk marked this conversation as resolved.
Show resolved Hide resolved
const svgString = qrElem.toSvgString(qrQuietZone);
return getSvgElement(svgString);
} catch (err) {
throw (err === "Data too long")
? new QrError.DataOverflowError() : err;
}
}
17 changes: 14 additions & 3 deletions src/popup/modules/UserInterface.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
* @requires /common/modules/lodash/isObject
* @requires /common/modules/lodash/throttle
* @requires /common/modules/data/MessageLevel
* @requires /common/modules/Logger
* @requires /common/modules/AddonSettings
* @requires /common/modules/MessageHandler
* @requires ./QrLib/QrErrors
* @requires ./QrCreator
*/
// lodash
Expand All @@ -19,6 +19,7 @@ import { COMMUNICATION_MESSAGE_TYPE } from "/common/modules/data/BrowserCommunic
import * as AddonSettings from "/common/modules/AddonSettings/AddonSettings.js";
import * as CommonMessages from "/common/modules/MessageHandler/CommonMessages.js";

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

Expand Down Expand Up @@ -108,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 text input is too long
if (e instanceof QrError.DataOverflowError) {
CommonMessages.showError("errorQrCodeOverflow");
console.error("Maximum size of QR code data exceeded with", text.length, "characters.");
} else {
throw e;
}
rugk marked this conversation as resolved.
Show resolved Hide resolved
}
}, QR_CODE_REFRESH_TIMEOUT);

/**
Expand Down
2 changes: 1 addition & 1 deletion src/popup/qrcode.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<link rel="stylesheet" href="../common/common.css">
<link rel="stylesheet" href="qrcode.css">

<script src="lib/kjua.min.js" charset="utf-8"></script>
<script src="lib/kjua-0.2.0.min.js" charset="utf-8"></script>
<script src="lib/qrcodegen.js" charset="utf-8"></script>

<script type="module" src="/common/modules/Logger/OverwriteConsoleLog.js" async></script>
Expand Down