From defbf625bdef0f8a666b72b8ce99de5e01af6b91 Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Thu, 29 Aug 2019 23:12:55 +0200 Subject: url-based pay/withdraw, use react hooks --- src/http.ts | 50 +++++++++++++++++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 15 deletions(-) (limited to 'src/http.ts') diff --git a/src/http.ts b/src/http.ts index f450d8479..8c1f772d8 100644 --- a/src/http.ts +++ b/src/http.ts @@ -27,7 +27,6 @@ export interface HttpResponse { responseJson: object & any; } - /** * The request library is bundled into an interface to make mocking easy. */ @@ -37,15 +36,16 @@ export interface HttpRequestLibrary { postJson(url: string, body: any): Promise; } - /** * An implementation of the [[HttpRequestLibrary]] using the * browser's XMLHttpRequest. */ export class BrowserHttpLib implements HttpRequestLibrary { - private req(method: string, - url: string, - options?: any): Promise { + private req( + method: string, + url: string, + options?: any, + ): Promise { return new Promise((resolve, reject) => { const myRequest = new XMLHttpRequest(); myRequest.open(method, url); @@ -54,11 +54,36 @@ export class BrowserHttpLib implements HttpRequestLibrary { } else { myRequest.send(); } - myRequest.addEventListener("readystatechange", (e) => { + + myRequest.onerror = e => { + console.error("http request error"); + reject(Error("could not make XMLHttpRequest")); + }; + + myRequest.addEventListener("readystatechange", e => { if (myRequest.readyState === XMLHttpRequest.DONE) { - const responseJson = JSON.parse(myRequest.responseText); + if (myRequest.status === 0) { + reject(Error("HTTP Request failed (status code 0, maybe URI scheme is wrong?)")) + return; + } + if (myRequest.status != 200) { + reject( + Error( + `HTTP Response with unexpected status code ${myRequest.status}: ${myRequest.statusText}`, + ), + ); + return; + } + let responseJson; + try { + responseJson = JSON.parse(myRequest.responseText); + } catch (e) { + reject(Error("Invalid JSON from HTTP response")); + return; + } if (responseJson === null || typeof responseJson !== "object") { reject(Error("Invalid JSON from HTTP response")); + return; } const resp = { responseJson: responseJson, @@ -70,27 +95,22 @@ export class BrowserHttpLib implements HttpRequestLibrary { }); } - get(url: string) { return this.req("get", url); } - postJson(url: string, body: any) { - return this.req("post", url, {req: JSON.stringify(body)}); + return this.req("post", url, { req: JSON.stringify(body) }); } - postForm(url: string, form: any) { - return this.req("post", url, {req: form}); + return this.req("post", url, { req: form }); } } - /** * Exception thrown on request errors. */ export class RequestException { - constructor(public detail: any) { - } + constructor(public detail: any) {} } -- cgit v1.2.3