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:
parent
1e469504e5
commit
1ecbb69937
@ -42,12 +42,15 @@ export default class Brizo {
|
||||
consumerAddress,
|
||||
}
|
||||
|
||||
return WebServiceConnectorProvider
|
||||
try {
|
||||
return await WebServiceConnectorProvider
|
||||
.getConnector()
|
||||
.post(
|
||||
this.getPurchaseEndpoint(),
|
||||
decodeURI(JSON.stringify(args)),
|
||||
)
|
||||
|
||||
} catch(e) {
|
||||
throw new Error("HTTP request failed")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -57,6 +57,7 @@ import config from "./config"
|
||||
|
||||
const service: Service = ddo.findServiceByType("Access")
|
||||
|
||||
try {
|
||||
await ocean
|
||||
.initializeServiceAgreement(
|
||||
ddo.id,
|
||||
@ -66,6 +67,10 @@ import config from "./config"
|
||||
(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)
|
||||
})()
|
||||
|
@ -1,7 +1,7 @@
|
||||
import GenericContract from "./contracts/GenericContract"
|
||||
import EventListener from "./EventListener"
|
||||
import Web3Provider from "./Web3Provider"
|
||||
// import Logger from "../utils/Logger"
|
||||
import Logger from "../utils/Logger"
|
||||
|
||||
export default class Event {
|
||||
|
||||
@ -40,6 +40,9 @@ export default class Event {
|
||||
private async handler(callback: any) {
|
||||
const contract = await GenericContract.getInstance(this.contractName)
|
||||
|
||||
if (this.lastBlock > await Web3Provider.getWeb3().eth.getBlockNumber()) {
|
||||
return
|
||||
}
|
||||
try {
|
||||
const events = await contract.getEventData(this.eventName, {
|
||||
filter: this.filter,
|
||||
@ -52,7 +55,7 @@ export default class Event {
|
||||
callback(events)
|
||||
}
|
||||
} catch (err) {
|
||||
// Logger.log("err")
|
||||
Logger.log(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +1,12 @@
|
||||
import fetch, { BodyInit, RequestInit, Response } from "node-fetch"
|
||||
import Logger from "./Logger"
|
||||
|
||||
/**
|
||||
* Provides a common interface to web services.
|
||||
*/
|
||||
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, {
|
||||
method: "POST",
|
||||
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, {
|
||||
method: "GET",
|
||||
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, {
|
||||
method: "PUT",
|
||||
body: payload,
|
||||
@ -35,6 +36,12 @@ export default class WebServiceConnector {
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user