Giter Site home page Giter Site logo

crash's Introduction

Crash

Build Status Join the chat at https://gitter.im/molovo/crash

Proper error handling, exceptions and try/catch for ZSH.

Crash Screenshot

Installation

zulu install crash

Manual

git clone https://github.com/molovo/crash crash
cp crash/crash /usr/local/share/zsh/site-functions # Or anywhere else in $fpath

Usage

Set up the global error handler

Crash comes with a global error handler, which prints a readable error and stack trace both for user-created exceptions and traditional shell exit codes.

autoload -Uz crash && crash register

throw TestException 'This is a test' # Will cause the error handler to be displayed

Try/catch

Crash comes with the functions try, catch and throw, allowing you to handle exceptions much as you would in a 'proper' programming language.

autoload -Uz crash && crash register

# The function we're going to call
function do_something() {
  echo 'Start to do something...'
  throw RainbowException 'Unicorns!'
  echo 'This message will never be displayed'
}

# A function to handle any caught exceptions
function error_handler() {
  local exception="$1" message="${(@)@:2}"

  echo $exception # RainbowException
  echo $message   # Unicorns!
}

try do_something
catch RainbowException error_handler

License

Copyright (c) 2016 James Dinsdale [email protected] (molovo.co)

Crash is licensed under The MIT License (MIT)

Team

crash's People

Contributors

molovo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

crash's Issues

Allow try/catch statements to be nested

This may or may not be possible. The try command needs to launch a subprocess to allow the exception to be caught, which means that modifying the global environment is not possible. To allow statements to be nested we'd need to maintain an 'exception stack' in an array, but cannot do this in its current form due to the use of a subprocess. Thrown exceptions are currently reported back to the global process by echoing the exception and message, so we could maintain the stack outside of the try block, but my first attempt at this didn't work. Something to look into.

Add unload command

The error handler fires on the occurrence of any non-zero exit code, but there are many instances where we want a program to exit gracefully without the stack trace being printed to the screen. Will add an unload command which will remove the error handler, so that it can be called prior to exciting.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.