From a1067de03230c8266ef5f16fea15ef899086621f Mon Sep 17 00:00:00 2001
From: Manuel Friedli <manuel@fritteli.ch>
Date: Wed, 21 Sep 2016 13:36:39 +0200
Subject: [PATCH] added specific error messages to converters.

---
 app/app.component.ts                    | 9 ++++-----
 app/converter/base64decoder.ts          | 7 ++++++-
 app/converter/base64encoder.ts          | 6 +++++-
 app/converter/bintodecconverter.ts      | 6 +++++-
 app/converter/dectobinconverter.ts      | 6 +++++-
 app/converter/dectohexconverter.ts      | 8 ++++++--
 app/converter/hextodecconverter.ts      | 8 ++++++--
 app/converter/quotedprintabledecoder.ts | 6 +++++-
 8 files changed, 42 insertions(+), 14 deletions(-)

diff --git a/app/app.component.ts b/app/app.component.ts
index 79d9816..8aee806 100644
--- a/app/app.component.ts
+++ b/app/app.component.ts
@@ -32,16 +32,15 @@ export class AppComponent extends OnInit {
             let result:string;
             try {
                 result = converter.convert(content);
-            } catch (error) {
+            } catch (error:Error) {
                 if (typeof console === "object" && typeof console.log === "function") {
                     console.log(error);
                 }
+                step.message = error.message;
+                step.error = true;
                 result = null;
             }
-            if (result === null) {
-                step.message = "Error converting. Not applicable?";
-                step.error = true;
-            } else {
+            if (result !== null) {
                 step.message = "";
                 step.error = false;
                 if (result !== "") {
diff --git a/app/converter/base64decoder.ts b/app/converter/base64decoder.ts
index 1b4e5b1..e80597c 100644
--- a/app/converter/base64decoder.ts
+++ b/app/converter/base64decoder.ts
@@ -1,4 +1,5 @@
 import {Converter} from "./converter";
+import log = require("core-js/core/log");
 
 export class Base64Decoder implements Converter {
     getDisplayname():string {
@@ -10,6 +11,10 @@ export class Base64Decoder implements Converter {
     }
 
     convert(input:string):string {
-        return atob(input);
+        try {
+            return atob(input);
+        } catch (exception) {
+            throw new Error("Could not decode base64 string. Maybe corrupt input?");
+        }
     }
 }
diff --git a/app/converter/base64encoder.ts b/app/converter/base64encoder.ts
index 409636d..989db7a 100644
--- a/app/converter/base64encoder.ts
+++ b/app/converter/base64encoder.ts
@@ -10,6 +10,10 @@ export class Base64Encoder implements Converter {
     }
 
     convert(input:string):string {
-        return btoa(input);
+        try {
+            return btoa(input);
+        } catch (exception) {
+            throw new Error("Could not encode base64 string. This should not happen, so why don't you just try again?");
+        }
     }
 }
diff --git a/app/converter/bintodecconverter.ts b/app/converter/bintodecconverter.ts
index 2c73959..0b4cbd7 100644
--- a/app/converter/bintodecconverter.ts
+++ b/app/converter/bintodecconverter.ts
@@ -9,6 +9,10 @@ export class BinToDecConverter implements Converter {
     }
 
     convert(input:string):string {
-        return parseInt(input, 2).toString(10);
+        let n:number = parseInt(input, 2);
+        if (isNaN(n)) {
+            throw new Error("The input seems not to be a valid binary number.");
+        }
+        return n.toString(10);
     }
 }
diff --git a/app/converter/dectobinconverter.ts b/app/converter/dectobinconverter.ts
index 8cc2b0d..b64a310 100644
--- a/app/converter/dectobinconverter.ts
+++ b/app/converter/dectobinconverter.ts
@@ -9,6 +9,10 @@ export class DecToBinConverter implements Converter {
     }
 
     convert(input:string):string {
-        return parseInt(input, 10).toString(2);
+        let n:number = parseInt(input, 10);
+        if (isNaN(n)) {
+            throw new Error("The input seems not to be a valid integer.");
+        }
+        return n.toString(2);
     }
 }
diff --git a/app/converter/dectohexconverter.ts b/app/converter/dectohexconverter.ts
index c0be47f..b033792 100644
--- a/app/converter/dectohexconverter.ts
+++ b/app/converter/dectohexconverter.ts
@@ -1,7 +1,7 @@
 import {Converter} from "./converter";
 export class DecToHexConverter implements Converter {
     getDisplayname():string {
-        return "Convert decimal to heximal";
+        return "Convert decimal to hexadecimal";
     }
 
     getId():string {
@@ -9,6 +9,10 @@ export class DecToHexConverter implements Converter {
     }
 
     convert(input:string):string {
-        return parseInt(input, 10).toString(16);
+        let n:number = parseInt(input, 10);
+        if (isNaN(n)) {
+            throw new Error("The input seems not to be a valid integer.");
+        }
+        return n.toString(16);
     }
 }
diff --git a/app/converter/hextodecconverter.ts b/app/converter/hextodecconverter.ts
index c6b3dba..2dcdad2 100644
--- a/app/converter/hextodecconverter.ts
+++ b/app/converter/hextodecconverter.ts
@@ -1,7 +1,7 @@
 import {Converter} from "./converter";
 export class HexToDecConverter implements Converter {
     getDisplayname():string {
-        return "Convert heximal to decimal";
+        return "Convert hexadecimal to decimal";
     }
 
     getId():string {
@@ -9,6 +9,10 @@ export class HexToDecConverter implements Converter {
     }
 
     convert(input:string):string {
-        return parseInt(input, 16).toString(10);
+        let n:number = parseInt(input, 16);
+        if (isNaN(n)) {
+            throw new Error("The input seems not to be a valid hexadecimal number.")
+        }
+        return n.toString(10);
     }
 }
diff --git a/app/converter/quotedprintabledecoder.ts b/app/converter/quotedprintabledecoder.ts
index 6787ff1..d998038 100644
--- a/app/converter/quotedprintabledecoder.ts
+++ b/app/converter/quotedprintabledecoder.ts
@@ -14,6 +14,10 @@ export class QuotedPrintableDecoder implements Converter {
     }
 
     convert(input:string):string {
-        return this.nativeLibraryWrapperService.utf8.decode(this.nativeLibraryWrapperService.quotedPrintable.decode(input));
+        try {
+            return this.nativeLibraryWrapperService.utf8.decode(this.nativeLibraryWrapperService.quotedPrintable.decode(input));
+        } catch (error) {
+            throw new Error("The input can not be interpreted as quoted-printable. May be corrupt?");
+        }
     }
 }