Templates/NewModule/src/Classes/Module.Errors.ps1

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
using namespace System.Management.Automation

<%
    if ($PLASTER_PARAM_Localization -eq 'yes') {
    @'
<#
    [Error]::PathNotFound($Path) is provided as an example below. To use these errors:
 
        throw [Terminating]
 
            throw [Error]::PathNotFound($LocalizedData.ErrorPathNotFound,$MyPath)
 
        $PSCmdlet.ThrowTerminatingError [Terminating]
 
            $PSCmdlet.ThrowTerminatingError([Error]::PathNotFound($LocalizedData.ErrorPathNotFound,$MyPath))
 
        $PSCmdlet.WriteError
 
            $PSCmdlet.WriteError([Error]::PathNotFound($LocalizedData.ErrorPathNotFound,$MyPath))
 
    Valid error categories:
 
        PS> [enum]::GetValues([System.Management.Automation.ErrorCategory])
#>
 
class Error {
    static [ErrorRecord] PathNotFound([String]$Exception,[string]$Path) {
        $Exp = [System.ArgumentException]::new($Exception -f $Path)
        $ErrorCategory = [ErrorCategory]::ObjectNotFound
        return [ErrorRecord]::new($Exp, 'PathNotFound', $ErrorCategory, $Path)
    }
}
'@

    }
    else {
    @'
<#
    [Error]::PathNotFound($Path) is provided as an example below. To use these errors:
 
        throw [Terminating]
 
            throw [Error]::PathNotFound($MyPath)
 
        $PSCmdlet.ThrowTerminatingError [Terminating]
 
            $PSCmdlet.ThrowTerminatingError([Error]::PathNotFound($MyPath))
 
        $PSCmdlet.WriteError
 
            $PSCmdlet.WriteError([Error]::PathNotFound($MyPath))
 
    Valid error categories:
 
        PS> [enum]::GetValues([System.Management.Automation.ErrorCategory])
#>
 
class Error {
    static [ErrorRecord] PathNotFound([string]$Path) {
        $Exception = [System.ArgumentException]::new("Cannot find path '$Path' because it does not exist.")
        $ErrorCategory = [ErrorCategory]::ObjectNotFound
        return [ErrorRecord]::new($Exception, 'PathNotFound', $ErrorCategory, $Path)
    }
}
'@

    }
%>