added specific error messages to converters.

This commit is contained in:
Manuel Friedli 2016-09-21 13:36:39 +02:00
parent 052897180b
commit a1067de032
8 changed files with 42 additions and 14 deletions

View file

@ -32,16 +32,15 @@ export class AppComponent extends OnInit {
let result:string; let result:string;
try { try {
result = converter.convert(content); result = converter.convert(content);
} catch (error) { } catch (error:Error) {
if (typeof console === "object" && typeof console.log === "function") { if (typeof console === "object" && typeof console.log === "function") {
console.log(error); console.log(error);
} }
step.message = error.message;
step.error = true;
result = null; result = null;
} }
if (result === null) { if (result !== null) {
step.message = "Error converting. Not applicable?";
step.error = true;
} else {
step.message = ""; step.message = "";
step.error = false; step.error = false;
if (result !== "") { if (result !== "") {

View file

@ -1,4 +1,5 @@
import {Converter} from "./converter"; import {Converter} from "./converter";
import log = require("core-js/core/log");
export class Base64Decoder implements Converter { export class Base64Decoder implements Converter {
getDisplayname():string { getDisplayname():string {
@ -10,6 +11,10 @@ export class Base64Decoder implements Converter {
} }
convert(input:string):string { convert(input:string):string {
return atob(input); try {
return atob(input);
} catch (exception) {
throw new Error("Could not decode base64 string. Maybe corrupt input?");
}
} }
} }

View file

@ -10,6 +10,10 @@ export class Base64Encoder implements Converter {
} }
convert(input:string):string { 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?");
}
} }
} }

View file

@ -9,6 +9,10 @@ export class BinToDecConverter implements Converter {
} }
convert(input:string):string { 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);
} }
} }

View file

@ -9,6 +9,10 @@ export class DecToBinConverter implements Converter {
} }
convert(input:string):string { 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);
} }
} }

View file

@ -1,7 +1,7 @@
import {Converter} from "./converter"; import {Converter} from "./converter";
export class DecToHexConverter implements Converter { export class DecToHexConverter implements Converter {
getDisplayname():string { getDisplayname():string {
return "Convert decimal to heximal"; return "Convert decimal to hexadecimal";
} }
getId():string { getId():string {
@ -9,6 +9,10 @@ export class DecToHexConverter implements Converter {
} }
convert(input:string):string { 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);
} }
} }

View file

@ -1,7 +1,7 @@
import {Converter} from "./converter"; import {Converter} from "./converter";
export class HexToDecConverter implements Converter { export class HexToDecConverter implements Converter {
getDisplayname():string { getDisplayname():string {
return "Convert heximal to decimal"; return "Convert hexadecimal to decimal";
} }
getId():string { getId():string {
@ -9,6 +9,10 @@ export class HexToDecConverter implements Converter {
} }
convert(input:string):string { 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);
} }
} }

View file

@ -14,6 +14,10 @@ export class QuotedPrintableDecoder implements Converter {
} }
convert(input:string):string { 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?");
}
} }
} }