about_Error.help.txt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
TOPIC
    about_Error
 
SHORT DESCRIPTION
    Error provides two important pieces of error/exception handling functionality that is
    missing or incomplete from PowerShell; identifying complex exceptions based on hash
    tables, and writing out detailed stack trace information in text form.
 
LONG 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.
 
REQUIREMENTS
    None.
 
EXAMPLE #1
    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.
 
EXAMPLE #2
    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.
 
EXAMPLE #3
    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.
 
EXAMPLE #4
    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.
 
LINKS
    https://github.com/codykonior/Error