Coder Social home page Coder Social logo

travitch / filelocation.hs Goto Github PK

View Code? Open in Web Editor NEW

This project forked from gregwebs/filelocation.hs

0.0 3.0 1.0 111 KB

error and debug function for haskell that give file locations

License: BSD 3-Clause "New" or "Revised" License

Haskell 92.44% C 6.74% Shell 0.82%

filelocation.hs's Introduction

Common debugging/error/exception functions and template haskell versions that give file location information

Error/undefined

> $(err "OH NO!")
main:Main main.hs:16:1 OH NO!

Notice how it displays package:module file:line:character It exposes the functions err (error), undef (undefined), and trc (Debug.Trace.trace). All of these behave the same as their normal counterpart but also spit out a location.

> $undef
main:Main test/main.hs:10:5 undefined
main: Prelude.undefined

2 other error functions are included- fromJst and fromRht.

> $fromJst Nothing
main:Main test/main.hs:27:4 fromJst: Nothing

Debug helpers

I also included my favorite helper, debug, which is like trace but just show the value.

> debug [1,2,3]
DEBUG: [1,2,3]
[1,2,3]

There is a version without the DEBUG prefix called both strace and traceId. And Here is the TH version of debug dbg:

> $dbg [1,2,3]
DEBUG main:Main main.hs:1:3 [1,2,3]
[1,2,3]

Additionally there is a debugMsg and a TH function dbgMsg can be used to add a message prefix:

> $(dbgMsg "the list") [1,2,3]
DEBUG main:Main main.hs:1:3 the list: [1,2,3]
[1,2,3]

Also there is debugM for stand-alone use in a monad. There is also functions ltrace and ltraceM (short for labeled trace)- it is like debug, but you tell it what string to print out instead of "DEBUG".

Exceptions

Also there is a version of thrwIO that gives location information. thrwsIO takes appends a string to the file locaiton information.

import Control.Exception.Base (Exception(..))
import Prelude hiding (catch)
import Control.Exception.Control (throwIO, catch)

data AException = AException String deriving (Show, Typeable)
instance Exception AException

($(thrwIO) AException) `catch` \e -> putStrLn ("Caught " ++ show (e :: AException))

Caught AException "main:Main test/main.hs:25:6"

Test Suite

./test/run.sh

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.