Display custom error page

Hey everyone!!

Been checking and playing around to see if there’s a way to display an error landing page. So far have been unsuccessful. Plan for now is something basic where when a param is null or empty for specific
details page to then load the error page with a custom message. Code snippet below of main dashboard script.

Any suggestion or idea is greatly appreciated :wink:


$PageFile = "C:\ProgramData\UniversalAutomation\Repository"

enum SecurityGroups {
    WebDeployerSecGroup
    TestGroup
}

$NavList = {
    if ([string]::IsNullOrEmpty($Session:userinfo.displayName)) {
        New-UDListItem -Label 'Hi Guest!' -Icon (New-UDIcon -Icon user -Size 2x)
    }
    else {
        New-UDListItem -Label "Hi $($Session:userinfo.displayName)!" -Icon (New-UDDynamic -Content {New-UDHTML -Markup "<img src='data:image/jpeg;base64,$($Session:userinfo.AvatarPic)' />"}) -Children {
            New-UDListItem -Label "Log Out!" -OnClick {
                $Session:userinfo = $null
                Show-UDToast -Message 'Logged out!'
                Invoke-UDRedirect http://localhost:5000/noc/login
            }
        }
    }

    if (!([string]::IsNullOrEmpty($Session:userinfo.displayName))) {
        if ($Session:userinfo.SecGroups.displayName | Where-Object {$_ -eq [SecurityGroups]::WebDeployerSecGroup}){
            New-UDListItem -Label "Web Deployer" -OnClick {Invoke-UDRedirect -Url './deployer'}
            New-UDListItem -Label "Web Deployer - Approval" -OnClick {Invoke-UDRedirect -Url './deployer/approval'}
        }
    }
    
    New-UDListItem -Label "Login" -OnClick {Invoke-UDRedirect -Url './login'}
}

$Pages = @()

$Pages += New-UDPage -Name 'Web Deployer' -Url "deployer" -Content {
    . "$($PageFile)\Pages\WebDeployerFORM.ps1"
} -NavigationLayout temporary -LoadNavigation $NavList

$Pages += New-UDPage -Name 'Login' -Url "login" -Content {
    . "$($PageFile)\Pages\Login.ps1"
} -NavigationLayout temporary -LoadNavigation $NavList

$Pages += New-UDPage -Name 'Web Deployer - Approval' -Url "deployer/approval" -Content {
    . "$($PageFile)\Pages\WebDeployerAPPROVAL.ps1"
} -NavigationLayout temporary -LoadNavigation $NavList

$Pages += New-UDPage -Name 'Web Deployer - View Request' -Url "deployer/request/view/:reqid" -Content {
    param(
        $reqid
    )
    if ([string]::IsNullOrEmpty($reqid) -or $reqid -eq " ") {
        . "$($PageFile)\Pages\Error\ErrorLanding.ps1" -ErrorMessage "No ID was provided. Please try again!"
    }
    else {
        . "$($PageFile)\Pages\WebDeployerReqView.ps1" -Id $reqid
    }
    
} -NavigationLayout temporary -LoadNavigation $NavList

# $Pages += New-UDPage -Name 'ERROR' -Url "error" -Content {
#     param(
#         $message
#     )
#     . "$($PageFile)\Pages\Error\ErrorLanding.ps1" -ErrorMessage $message
# } -NavigationLayout temporary -LoadNavigation $NavList

New-UDDashboard -Title 'NOC Dash' -Pages $Pages -Theme (. "$($PageFile)\Styling\Theme.ps1")

ErrorLanding.ps1

param(
    [string]$ErrorMessage
)

function New-CenterContent {
    param($Content)

    New-UDElement -tag div -Attributes @{ style = @{ position = 'absolute'; top = '50%'; Left = '50%'; transform = 'translate(-50%, -50%)'; } } -Content $Content
}

New-CenterContent -Content {
    New-UDElement -Id 'viewerror' -Tag 'span' -Endpoint {
        New-UDIcon -Icon 'dizzy' -Size 4x #-Style @{ color = 'red' }
        New-UDTypography -Text "$($ErrorMessage)" -Variant h3 -Style @{ color = 'red' }
    } -Attributes @{
        style = @{
            display = 'inline-flex'
        }
    }
}
Product: PowerShell Universal
Version: 2.0.3

Checking in if anyone has an idea on how to accomplish this if possible ?

Your code seems like it should work. What issue are you having?

It gets ignored from what I see and display PSU dashboard page not found

Ah. It might be because the :reqid isn’t specified so the route doesn’t match. You could try something like this.

$Pages += New-UDPage -Name 'Web Deployer - View Request' -Url "deployer/request/view" -Content {
        . "$($PageFile)\Pages\Error\ErrorLanding.ps1" -ErrorMessage "No ID was provided. Please try again!"
} -NavigationLayout temporary -LoadNavigation $NavList

$Pages += New-UDPage -Name 'Web Deployer - View Request' -Url "deployer/request/view/:reqid" -Content {
    param(
        $reqid
    )
    if ([string]::IsNullOrEmpty($reqid) -or $reqid -eq " ") {
        . "$($PageFile)\Pages\Error\ErrorLanding.ps1" -ErrorMessage "No ID was provided. Please try again!"
    }
    else {
        . "$($PageFile)\Pages\WebDeployerReqView.ps1" -Id $reqid
    }
    
} -NavigationLayout temporary -LoadNavigation $NavList

Ohh I see. I’ll give it a try in a few and let you know.

Thanks for the help!

Great stuff!! Something so simple :slight_smile:

1 Like