Improve sip API error handling
This commit is contained in:
		
							parent
							
								
									8bc1eaebc0
								
							
						
					
					
						commit
						6852319e4d
					
				@ -1,5 +1,17 @@
 | 
				
			|||||||
import fetch from 'node-fetch-commonjs';
 | 
					import fetch from 'node-fetch-commonjs';
 | 
				
			||||||
import config from './config';
 | 
					import config from './config';
 | 
				
			||||||
 | 
					import http from 'node:http';
 | 
				
			||||||
 | 
					import https from 'node:https';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const httpAgent = new http.Agent({ keepAlive: true });
 | 
				
			||||||
 | 
					const httpsAgent = new https.Agent({ keepAlive: true });
 | 
				
			||||||
 | 
					const agentSelector = function(_parsedURL: any) {
 | 
				
			||||||
 | 
					    if (_parsedURL.protocol == 'http:') {
 | 
				
			||||||
 | 
					        return httpAgent;
 | 
				
			||||||
 | 
					    } else {
 | 
				
			||||||
 | 
					        return httpsAgent;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
interface Match {
 | 
					interface Match {
 | 
				
			||||||
  render: boolean;
 | 
					  render: boolean;
 | 
				
			||||||
@ -16,6 +28,15 @@ interface SipTemplate {
 | 
				
			|||||||
  getData: Function;
 | 
					  getData: Function;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					async function sipFetchJSON(url, defaultVal = null) {
 | 
				
			||||||
 | 
					  try {
 | 
				
			||||||
 | 
					    const response = await fetch(url, { agent: agentSelector });
 | 
				
			||||||
 | 
					    return response.ok ? response.json() : defaultVal;
 | 
				
			||||||
 | 
					  } catch (error) {
 | 
				
			||||||
 | 
					    return defaultVal;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const routes = {
 | 
					const routes = {
 | 
				
			||||||
  about: {
 | 
					  about: {
 | 
				
			||||||
    title: "About",
 | 
					    title: "About",
 | 
				
			||||||
@ -41,11 +62,11 @@ const routes = {
 | 
				
			|||||||
        if (params?.length) {
 | 
					        if (params?.length) {
 | 
				
			||||||
          let blockId = params[0];
 | 
					          let blockId = params[0];
 | 
				
			||||||
          if (blockId.length !== 64) {
 | 
					          if (blockId.length !== 64) {
 | 
				
			||||||
            blockId = await (await fetch(config.API.ESPLORA + `/block-height/${blockId}`)).text();
 | 
					            blockId = await (await fetch(config.API.ESPLORA + `/block-height/${blockId}`, { agent: agentSelector })).text();
 | 
				
			||||||
          }
 | 
					          }
 | 
				
			||||||
          const [block, transactions] = await Promise.all([
 | 
					          const [block, transactions] = await Promise.all([
 | 
				
			||||||
            (await fetch(config.API.MEMPOOL + `/block/${blockId}`)).json(),
 | 
					            sipFetchJSON(config.API.MEMPOOL + `/block/${blockId}`),
 | 
				
			||||||
            (await fetch(config.API.ESPLORA + `/block/${blockId}/txids`)).json()
 | 
					            sipFetchJSON(config.API.ESPLORA + `/block/${blockId}/txids`),
 | 
				
			||||||
          ])
 | 
					          ])
 | 
				
			||||||
          return {
 | 
					          return {
 | 
				
			||||||
            block,
 | 
					            block,
 | 
				
			||||||
 | 
				
			|||||||
@ -79,12 +79,14 @@
 | 
				
			|||||||
          <td><%= data.block.merkle_root %></td>
 | 
					          <td><%= data.block.merkle_root %></td>
 | 
				
			||||||
        </tr>
 | 
					        </tr>
 | 
				
			||||||
      </table>
 | 
					      </table>
 | 
				
			||||||
      <h2><%= data.transactions.length %> Transactions</h2>
 | 
					      <% if (data.transactions) { %>
 | 
				
			||||||
      <ol>
 | 
					        <h2><%= data.transactions.length %> Transactions</h2>
 | 
				
			||||||
        <% data.transactions.forEach((txid, i) => { %>
 | 
					        <ol>
 | 
				
			||||||
          <li><a href="/tx/<%= txid %>"><%= txid %></a><% if (i === 0) { %> (Coinbase)<% } %></li>
 | 
					          <% data.transactions.forEach((txid, i) => { %>
 | 
				
			||||||
        <% }); %>
 | 
					            <li><a href="/tx/<%= txid %>"><%= txid %></a><% if (i === 0) { %> (Coinbase)<% } %></li>
 | 
				
			||||||
      </ol>
 | 
					          <% }); %>
 | 
				
			||||||
 | 
					        </ol>
 | 
				
			||||||
 | 
					      <% } %>
 | 
				
			||||||
    </div>
 | 
					    </div>
 | 
				
			||||||
    <%- include('footer'); %>
 | 
					    <%- include('footer'); %>
 | 
				
			||||||
  </body>
 | 
					  </body>
 | 
				
			||||||
 | 
				
			|||||||
@ -1,3 +1,4 @@
 | 
				
			|||||||
 | 
					<br><hr><br>
 | 
				
			||||||
<app-global-footer>
 | 
					<app-global-footer>
 | 
				
			||||||
  <footer>
 | 
					  <footer>
 | 
				
			||||||
    <div class="container-fluid">
 | 
					    <div class="container-fluid">
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user