1
0
mirror of https://github.com/oceanprotocol-archive/squid-js.git synced 2024-02-02 15:31:51 +01:00

improved error detection on http requests #118

This commit is contained in:
Pedro Gutiérrez 2019-01-23 12:29:13 +01:00
parent 1e469504e5
commit 1ecbb69937
4 changed files with 40 additions and 22 deletions

View File

@ -42,12 +42,15 @@ export default class Brizo {
consumerAddress, consumerAddress,
} }
return WebServiceConnectorProvider try {
.getConnector() return await WebServiceConnectorProvider
.post( .getConnector()
this.getPurchaseEndpoint(), .post(
decodeURI(JSON.stringify(args)), this.getPurchaseEndpoint(),
) decodeURI(JSON.stringify(args)),
)
} catch(e) {
throw new Error("HTTP request failed")
}
} }
} }

View File

@ -57,15 +57,20 @@ import config from "./config"
const service: Service = ddo.findServiceByType("Access") const service: Service = ddo.findServiceByType("Access")
await ocean try {
.initializeServiceAgreement( await ocean
ddo.id, .initializeServiceAgreement(
service.serviceDefinitionId, ddo.id,
serviceAgreementSignatureResult.serviceAgreementId, service.serviceDefinitionId,
serviceAgreementSignatureResult.serviceAgreementSignature, serviceAgreementSignatureResult.serviceAgreementId,
(files) => Logger.log(`Got files, first files length in bytes: ${files[0].length}`), serviceAgreementSignatureResult.serviceAgreementSignature,
consumer, (files) => Logger.log(`Got files, first files length in bytes: ${files[0].length}`),
) consumer,
)
} catch(e) {
Logger.warn("Error on initializeServiceAgreement:", e)
process.exit(1)
}
process.exit(0) process.exit(0)
})() })()

View File

@ -1,7 +1,7 @@
import GenericContract from "./contracts/GenericContract" import GenericContract from "./contracts/GenericContract"
import EventListener from "./EventListener" import EventListener from "./EventListener"
import Web3Provider from "./Web3Provider" import Web3Provider from "./Web3Provider"
// import Logger from "../utils/Logger" import Logger from "../utils/Logger"
export default class Event { export default class Event {
@ -40,6 +40,9 @@ export default class Event {
private async handler(callback: any) { private async handler(callback: any) {
const contract = await GenericContract.getInstance(this.contractName) const contract = await GenericContract.getInstance(this.contractName)
if (this.lastBlock > await Web3Provider.getWeb3().eth.getBlockNumber()) {
return
}
try { try {
const events = await contract.getEventData(this.eventName, { const events = await contract.getEventData(this.eventName, {
filter: this.filter, filter: this.filter,
@ -52,7 +55,7 @@ export default class Event {
callback(events) callback(events)
} }
} catch (err) { } catch (err) {
// Logger.log("err") Logger.log(err)
} }
} }
} }

View File

@ -1,11 +1,12 @@
import fetch, { BodyInit, RequestInit, Response } from "node-fetch" import fetch, { BodyInit, RequestInit, Response } from "node-fetch"
import Logger from "./Logger"
/** /**
* Provides a common interface to web services. * Provides a common interface to web services.
*/ */
export default class WebServiceConnector { export default class WebServiceConnector {
public async post(url: string, payload: BodyInit): Promise<Response> { public post(url: string, payload: BodyInit): Promise<Response> {
return this.fetch(url, { return this.fetch(url, {
method: "POST", method: "POST",
body: payload, body: payload,
@ -15,7 +16,7 @@ export default class WebServiceConnector {
}) })
} }
public async get(url: string): Promise<Response> { public get(url: string): Promise<Response> {
return this.fetch(url, { return this.fetch(url, {
method: "GET", method: "GET",
headers: { headers: {
@ -24,7 +25,7 @@ export default class WebServiceConnector {
}) })
} }
public async put(url: string, payload: BodyInit): Promise<Response> { public put(url: string, payload: BodyInit): Promise<Response> {
return this.fetch(url, { return this.fetch(url, {
method: "PUT", method: "PUT",
body: payload, body: payload,
@ -35,6 +36,12 @@ export default class WebServiceConnector {
} }
private async fetch(url: string, opts: RequestInit): Promise<Response> { private async fetch(url: string, opts: RequestInit): Promise<Response> {
return fetch(url, opts) const result = await fetch(url, opts);
if (!result.ok) {
Logger.warn(`Error requesting [${opts.method}] ${url}`)
Logger.warn(`Response message: \n${await result.text()}`)
throw result
}
return result
} }
} }