From bbf9b0c00864b29640c01cb58c0c688df77beb12 Mon Sep 17 00:00:00 2001 From: Fabian Stamm Date: Sat, 10 Mar 2018 18:33:47 +0100 Subject: [PATCH] Adding offline exception --- index.d.ts | 4 ++++ index.js | 38 +++++++++++++++++++++++++++++++++----- index.ts | 25 ++++++++++++++++++++----- package.json | 2 +- 4 files changed, 58 insertions(+), 11 deletions(-) diff --git a/index.d.ts b/index.d.ts index 227f156..983ee1e 100644 --- a/index.d.ts +++ b/index.d.ts @@ -30,6 +30,10 @@ export default class SecureFile { delete(id: string): Promise; history(id: string): Promise; } +export declare class NoConnection extends Error { + type: string; + constructor(); +} export declare class Unauthorized extends Error { type: string; constructor(); diff --git a/index.js b/index.js index 0775ef1..d197811 100644 --- a/index.js +++ b/index.js @@ -62,7 +62,7 @@ var SecureFile = /** @class */ (function () { } SecureFile.prototype.getCode = function () { return __awaiter(this, void 0, void 0, function () { - var myHeaders, myInit, code_res, code, r; + var myHeaders, myInit, code_res, err_1, code, r; return __generator(this, function (_a) { switch (_a.label) { case 0: @@ -73,12 +73,21 @@ var SecureFile = /** @class */ (function () { method: 'GET', headers: myHeaders, }; - return [4 /*yield*/, fetch(this.Server + "/code?username=" + this.Username, myInit)]; + _a.label = 1; case 1: + _a.trys.push([1, 3, , 4]); + return [4 /*yield*/, fetch(this.Server + "/code?username=" + this.Username, myInit)]; + case 2: code_res = _a.sent(); + return [3 /*break*/, 4]; + case 3: + err_1 = _a.sent(); + //TODO probably better fail check + throw new NoConnection(); + case 4: statusParser(code_res); return [4 /*yield*/, code_res.json()]; - case 2: + case 5: code = (_a.sent()).code; r = new rsa(this.PrivateKey, "pkcs1-pem"); return [2 /*return*/, { code: code, signature: r.sign(code).toString("base64") }]; @@ -88,7 +97,7 @@ var SecureFile = /** @class */ (function () { }; SecureFile.prototype.makeRequest = function (endpoint, method, query, body) { return __awaiter(this, void 0, void 0, function () { - var code, query_str, first, key, myHeaders; + var code, query_str, first, key, myHeaders, res, err_2; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.getCode()]; @@ -107,8 +116,17 @@ var SecureFile = /** @class */ (function () { myHeaders = new Headers(); myHeaders.append('pragma', 'no-cache'); myHeaders.append('cache-control', 'no-cache'); + _a.label = 2; + case 2: + _a.trys.push([2, 4, , 5]); return [4 /*yield*/, fetch(this.Server + endpoint + query_str, { method: method, body: body, headers: myHeaders })]; - case 2: return [2 /*return*/, _a.sent()]; + case 3: + res = _a.sent(); + return [2 /*return*/, res]; + case 4: + err_2 = _a.sent(); + throw new NoConnection(); + case 5: return [2 /*return*/]; } }); }); @@ -267,6 +285,16 @@ var SecureFile = /** @class */ (function () { return SecureFile; }()); exports.default = SecureFile; +var NoConnection = /** @class */ (function (_super) { + __extends(NoConnection, _super); + function NoConnection() { + var _this = _super.call(this, "No connection") || this; + _this.type = "noconnection"; + return _this; + } + return NoConnection; +}(Error)); +exports.NoConnection = NoConnection; var Unauthorized = /** @class */ (function (_super) { __extends(Unauthorized, _super); function Unauthorized() { diff --git a/index.ts b/index.ts index 856a135..6c5590c 100644 --- a/index.ts +++ b/index.ts @@ -41,10 +41,13 @@ export default class SecureFile { method: 'GET', headers: myHeaders, }; - - let code_res = await fetch(this.Server + "/code?username=" + this.Username, myInit); + try { + var code_res = await fetch(this.Server + "/code?username=" + this.Username, myInit); + } catch (err) { + //TODO probably better fail check + throw new NoConnection(); + } statusParser(code_res); - //ToDo check status Codes let code = (await code_res.json()).code; let r = new rsa(this.PrivateKey, "pkcs1-pem"); return { code: code, signature: r.sign(code).toString("base64") }; @@ -64,8 +67,12 @@ export default class SecureFile { var myHeaders = new Headers(); myHeaders.append('pragma', 'no-cache'); myHeaders.append('cache-control', 'no-cache'); - - return await fetch(this.Server + endpoint + query_str, { method: method, body: body, headers: myHeaders }); + try { + let res = await fetch(this.Server + endpoint + query_str, { method: method, body: body, headers: myHeaders }); + return res; + } catch (err) { + throw new NoConnection(); + } } async test(): Promise<{ user: string, test: true }> { @@ -138,6 +145,14 @@ export default class SecureFile { } } +export class NoConnection extends Error { + type: string; + constructor() { + super("No connection"); + this.type = "noconnection" + } +} + export class Unauthorized extends Error { type: string; constructor() { diff --git a/package.json b/package.json index 210a994..fa8aa3a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "secure-file-wrapper", - "version": "1.0.9", + "version": "1.0.10", "main": "index.js", "author": "Fabian Stamm ", "license": "MIT",