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 11 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/QrError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Errors in QR code generation
*
* @module QrLib/QrErrors
ongspxm marked this conversation as resolved.
Show resolved Hide resolved
*/

export class DataOverflowError extends Error {
constructor(...params) {
super(...params);
this.message = 'data overflow error';
ongspxm marked this conversation as resolved.
Show resolved Hide resolved
}
}
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;
ongspxm marked this conversation as resolved.
Show resolved Hide resolved
}
}

/**
Expand Down
11 changes: 9 additions & 2 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,7 +100,13 @@ export function set(tag, value) {
export function getQr() {
console.info("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 @@ -19,6 +20,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 +110,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