Skip to content

Commit

Permalink
Throw OperationError instead of returning a String
Browse files Browse the repository at this point in the history
  • Loading branch information
Flavsditz committed Jan 29, 2020
1 parent 1509b2b commit 0ab96dd
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
7 changes: 4 additions & 3 deletions src/core/operations/RailFenceCipherDecode.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

import Operation from "../Operation.mjs";
import OperationError from "../errors/OperationError.mjs";

/**
* Rail Fence Cipher Decode operation
Expand Down Expand Up @@ -48,13 +49,13 @@ class RailFenceCipherDecode extends Operation {
let cipher = input;

if (key < 2) {
return "Key has to be bigger than 2";
throw new OperationError("Key has to be bigger than 2");
} else if (key > cipher.length) {
return "Key should be smaller than the cipher's length";
throw new OperationError("Key should be smaller than the cipher's length");
}

if (offset < 0) {
return "Offset has to be a positive integer";
throw new OperationError("Offset has to be a positive integer");
}

const cycle = (key - 1) * 2;
Expand Down
7 changes: 4 additions & 3 deletions src/core/operations/RailFenceCipherEncode.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

import Operation from "../Operation.mjs";
import OperationError from "../errors/OperationError.mjs";

/**
* Rail Fence Cipher Encode operation
Expand Down Expand Up @@ -47,13 +48,13 @@ class RailFenceCipherEncode extends Operation {

const plaintext = input;
if (key < 2) {
return "Key has to be bigger than 2";
throw new OperationError("Key has to be bigger than 2");
} else if (key > plaintext.length) {
return "Key should be smaller than the plain text's length";
throw new OperationError("Key should be smaller than the plain text's length");
}

if (offset < 0) {
return "Offset has to be a positive integer";
throw new OperationError("Offset has to be a positive integer");
}

const cycle = (key - 1) * 2;
Expand Down

0 comments on commit 0ab96dd

Please sign in to comment.