diff --git a/src/core/operations/LuhnChecksum.mjs b/src/core/operations/LuhnChecksum.mjs index 58e904fb39..7b4f3b5967 100644 --- a/src/core/operations/LuhnChecksum.mjs +++ b/src/core/operations/LuhnChecksum.mjs @@ -81,8 +81,15 @@ class LuhnChecksum extends Operation { throw new OperationError("Error: Radix argument must be divisible by 2"); } - const checkSum = this.checksum(input, radix).toString(radix); - let checkDigit = this.checksum(input + "0", radix); + let checkSum, checkDigit; + + try { + checkSum = this.checksum(input, radix).toString(radix); + checkDigit = this.checksum(input + "0", radix); + } catch (err) { + throw new OperationError(err.message); + } + checkDigit = checkDigit === 0 ? 0 : (radix - checkDigit); checkDigit = checkDigit.toString(radix); diff --git a/tests/operations/tests/LuhnChecksum.mjs b/tests/operations/tests/LuhnChecksum.mjs index 33d7372e85..fe6953fd37 100644 --- a/tests/operations/tests/LuhnChecksum.mjs +++ b/tests/operations/tests/LuhnChecksum.mjs @@ -433,4 +433,15 @@ TestRegister.addTests([ }, ], }, + { + name: "Luhn Checksum on invalid radix character", + input: "o", + expectedOutput: "Character: o is not valid in radix 4.", + recipeConfig: [ + { + op: "Luhn Checksum", + args: [4] + }, + ], + }, ]);