Giter Site home page Giter Site logo

error's Introduction

Error PowerShell Module by Cody Konior

There is no logo yet.

Build status

Read the CHANGELOG

Description

PowerShell provides a simple try { } catch (type) { } exception handling mechanic, but due to the way exceptions are often wrapped in other exceptions and error records, it is often the case that these will be missed.

Furthermore when catching SQL Server exceptions these have an object nested at an arbitrary depth with important properties to indicate what kind of exception it is (such as severity, error number, message, state).

Together these two things lead to extremely long and complex catch blocks. To address this a Test-Error function is provided.

  • Test-Error -Type to identify an exception based on a type; matching not just the top object type but other common objects nested within it.
  • Test-Error -Property to identify an exception based on a hash table of matching properties on any single nested object within the error.

Another function, Resolve-Error, either returns an expanded view of nested objects in an exception, and optionally, converts this into a verbose (but almost readable) string output suitable for use in stack traces.

Installation

  • Install-Module Error

Major functions

  • Resolve-Error

Examples

    try {
        $ErrorActionPreference = "Stop"
        Invoke-Sqlcmd -ServerInstance C1N1 -Query "Select 1/0" -IncludeSqlUserErrors
    } catch [System.Data.SqlClient.SqlException] {
        "Caught"
    }

No output. This is because that exception type, despite being thrown, was buried.

try {
    $ErrorActionPreference = "Stop"
    Invoke-Sqlcmd -ServerInstance C1N1 -Query "Select 1/0" -IncludeSqlUserErrors
} catch {
    if (Test-Error System.Data.SqlClient.SqlException) {
        "Caught"
    } else {
        "Not caught"
    }
}

Results in a Caught output because we successfully matched a nested exception.

try {
    $ErrorActionPreference = "Stop"
    Invoke-Sqlcmd -ServerInstance C1N1 -Query "Select 1/0" -IncludeSqlUserErrors
} catch {
    if (Test-Error @{ Class = 16; Number = 8134; }) {
        "Caught"
    } else {
        "Not caught"
    }
}

Results in a Caught output because we successfully matched a set of properties.

try {
    $ErrorActionPreference = "Stop"
    Invoke-Sqlcmd -ServerInstance C1N1 -Query "Select 1/0" -IncludeSqlUserErrors
} catch {
    Resolve-Error $_ -AsString
}

A detailed string-style expansion of exceptions, error records, and the stack trace.

error's People

Contributors

codykonior avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

yevrag35

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.