Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@soinlabs/hawk

Package Overview
Dependencies
Maintainers
6
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@soinlabs/hawk

Package to better manage errors, logs, and its notifications

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
6
Created
Source

Hawk

codecov npm version npm downloads

Error and logs framework for Soin Labs projects.

How to use

For getting a simple use of Hawk, follow the steps bellow:

  1. Export the Hawk constructor and make an instance:

    // your index.js to handle hawk singleton configuration
    
    const { Hawk } = require("@soinlabs/hawk");
    
    const customizedHawk = new Hawk({
      logsPath: "yourWorkspace/route/example/logsDirectory",
      slackToken: "yourWebhookChannelURL",
      appName: "yourApplicationName",
      optionalSign: "optionalBenchmark",
    });
    
  2. Use your Hawk instance to perform your logs

    customizedHawk.log("My first log");
    
    const newError = customizedHawk.CreateElasticError("message", 300);
    customizedHawk.log(newError);
    
    // It is possible to perform a simple log with logging level
    const { Logger } = require("@soinlabs/hawk");
    customizedHawk.log("My first log", Logger.SILLY);
    
  3. Some error and logs handling utilities

    // Some error and logs handling utilities:
    
    const { Logger, LoggedError } = require('@soinlabs/hawk')
    
    const LoggingLevels = {
        error: Logger.ERROR,
        warn: Logger.WARN,
        info: Logger.INFO,
        http: Logger.HTTP,
        verbose: Logger.VERBOSE,
        debug: Logger.DEBUG,
        silly: Logger.SILLY,
    };
    
    const ErrorLevels = {
        low: LoggedError.LOW_LEVEL,
        medium: LoggedError.MEDIUM_LEVEL,
        high: LoggedError.HIGH_LEVEL,
    };
    
    /**
    * It creates an Elastic Error with message and code, after, it makes log with this error
    * and throw the created error as an exception
    * @param {String} message what happened?
    * @param {Number} status status code
    * @param {Number} errorLevel optional
    */
    function sendError(message, status, errorLevel = ErrorLevels.low) {
        const error = customizedHawk.CreateElasticError(message, status);
        error.setErrorLevel(errorLevel);
        customizedHawk.log(error, LoggingLevels.error);
        throw error;
    }
    
    /**
    * It performs a log with this message at warning level
    * @param {String} msg
    */
    function sendWarning(msg) {
        customizedHawk.log(
            { message: msg, referenceCode: customizedHawk.getReferenceCode() },
            LoggingLevels.warn
        )
    }
    
    /**
     * It creates an Elastic Error with message and code, after, it makes log with this error
    * @param {String} message
    * @param {Number} code
    * @param {Number} errorLevel optional
    */
    function logError(message, code, errorLevel = ErrorLevels.low) {
        const error = customizedHawk.CreateElasticError(message, code)
        customizedHawk.log(error, LoggingLevels.error)
    }
    
    ...
    // Use your utilies
    
    // This throw an exception
    sendError("No id provided", 400);
    
    // It performs a warning level log
    sendWarning('Bad request')
    

Another alternatives

You could do some things like these:

  1. Throw LoggedError

    // Use builder pattern
    
    throw new LoggedError()
      .setMessage(`Error captured by hawk: ${message}`)
      .setReferenceCode("myFile.js")
      .setErrorLevel(LoggedError.HIGH_LEVEL)
      .setToLog(true)
      .setToSlack(true);
    
    // Another way
    
    throw new LoggedError({
      message: `Error captured by hawk: ${message}`,
      status: 401,
      toLog: true,
      toElastic: false,
      toSlack: true,
      newField: "yourNewFieldValue",
    });
    

Keywords

FAQs

Package last updated on 05 Jun 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

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc