You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 7-8.RSVP
Socket
Socket
Sign inDemoInstall

@octokit/request-error

Package Overview
Dependencies
Maintainers
4
Versions
40
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@octokit/request-error

Error class for Octokit request errors


Version published
Weekly downloads
10M
decreased by-2.54%
Maintainers
4
Install size
4.30 MB
Created
Weekly downloads
 

Package description

What is @octokit/request-error?

The @octokit/request-error npm package is designed to handle errors that occur during requests made using the Octokit library, which is a GitHub API client. It provides a structured way to catch and process errors, making it easier to debug and handle exceptions in applications that interact with GitHub's API.

What are @octokit/request-error's main functionalities?

Creating a RequestError instance

This feature allows developers to create an instance of RequestError, providing a message, status code, and request details. This can be useful for simulating errors in tests or handling errors in a structured way.

const { RequestError } = require('@octokit/request-error');

const error = new RequestError('Message describing the error', 404, {
  request: {
    method: 'GET',
    url: 'https://api.github.com/repos/octocat/Hello-World',
    headers: {
      authorization: 'token secret123'
    }
  }
});

Accessing error details

Once a RequestError instance is created, developers can access various details about the error, such as the HTTP status code, error message, and the request that caused the error. This is particularly useful for logging and debugging purposes.

const error = new RequestError('Not Found', 404, {
  request: {
    method: 'GET',
    url: 'https://api.github.com/repos/octocat/Hello-World',
    headers: {
      authorization: 'token secret123'
    }
  }
});

console.log(error.status); // 404
console.log(error.message); // Not Found
console.log(error.request.method); // GET

Other packages similar to @octokit/request-error

Readme

Source

request-error.js

Error class for Octokit request errors

@latest Build Status

Usage

Browsers Load @octokit/request-error directly from esm.sh
<script type="module">
import { RequestError } from "https://esm.sh/@octokit/request-error";
</script>
Node

Install with npm install @octokit/request-error

import { RequestError } from "@octokit/request-error";

[!IMPORTANT] As we use conditional exports, you will need to adapt your tsconfig.json by setting "moduleResolution": "node16", "module": "node16".

See the TypeScript docs on package.json "exports".
See this helpful guide on transitioning to ESM from @sindresorhus

const error = new RequestError("Oops", 500, {
  request: {
    method: "POST",
    url: "https://api.github.com/foo",
    body: {
      bar: "baz",
    },
    headers: {
      authorization: "token secret123",
    },
  },
  response: {
    status: 500,
    url: "https://api.github.com/foo",
    headers: {
      "x-github-request-id": "1:2:3:4",
    },
    data: {
      foo: "bar",
    },
  },
});

error.message; // Oops
error.status; // 500
error.request; // { method, url, headers, body }
error.response; // { url, status, headers, data }

Usage with Octokit

try {
  // your code here that sends at least one Octokit request
  await octokit.request("GET /");
} catch (error) {
  // Octokit errors always have a `error.status` property which is the http response code
  if (error.status) {
    // handle Octokit error
  } else {
    // handle all other errors
    throw error;
  }
}

LICENSE

MIT

Keywords

FAQs

Package last updated on 11 Jul 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc