> For the complete documentation index, see [llms.txt](https://docs.devolutions.net/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.devolutions.net/powershell-universal/da/api/error-handling.md).

# Fejlhåndtering

Håndter fejl i REST API'er i PowerShell Universal ved at konfigurere ErrorAction, returnere brugerdefinerede statuskoder og læse fejl i Windows PowerShell.

Som standard returnerer slutpunkter en 200 OK-meddelelse, selv om der er fejl. Hvis der opstår en fejl, får De et tomt svar fra slutpunktet. Dette dokument viser forskellige måder at håndtere fejl på i API'er.

## Automatisk returnering af fejl

Hvis De automatisk vil returnere fejl fra API'er, kan De ændre standardfunktionsmåden ved at angive parameteren `-ErrorAction` for `New-PSUEndpoint` til `Stop`. Eventuelle fejl medfører, at der returneres en 500 Internal Server Error med en liste over fejlene og staksporingen.

Afsluttende fejl returnerer altid en 500 Internal Server Error.

```powershell
New-PSUEndpoint -Url "/error" -Endpoint { 
   throw "Uh oh!"
} -ErrorAction stop

New-PSUEndpoint -Url /error2 -Endpoint {
    Write-Error "Whoa!"
} -ErrorAction Stop
```

De vil bemærke forskellig funktionsmåde i Windows PowerShell og PowerShell 7, når De kalder REST API'er, der returnerer fejl. I Windows PowerShell modtager De en generisk fejl, der ikke returnerer fejlmeddelelsen.

```powershell
PS C:\Users\adamr> invoke-restmethod http://localhost:5000/error2
invoke-restmethod : The remote server returned an error: (500) Internal Server Error.
At line:1 char:1
+ invoke-restmethod http://localhost:5000/error2
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], Web
   Exception
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
```

I PowerShell 7, når der returneres en fejl, vil De se den returnerede fejlmeddelelse.

```powershell
PS C:\Users\adamr\Desktop> invoke-restmethod http://localhost:5000/error 

Invoke-RestMethod: Uh oh!
at , : line 2
at , : line 1

PS C:\Users\adamr\Desktop> invoke-restmethod http://localhost:5000/error2

Invoke-RestMethod: Whoa
at , : line 2
at , : line 1
```

De kan hente fejlmeddelelsen i Windows PowerShell ved at bruge følgende syntaks.

```powershell
PS C:\Users\adamr> try { invoke-restmethod http://localhost:5000/error2 } catch { [System.IO.StreamReader]::new($_.Exception.Response.GetResponseStream()).ReadToEnd()}
Whoa!
at <ScriptBlock>, <No file>: line 2
at <ScriptBlock>, <No file>: line 1
```

## Manuel returnering af fejl

For at returnere fejl manuelt skal De bruge cmdletten `New-PSUApiResponse`. Denne cmdlet giver Dem mulighed for at definere statuskoden og brødteksten for svaret.

I dette eksempel returnerer vi en 404-fejlkode fra slutpunktet.

```powershell
New-PSUEndpoint -Url /broken -Endpoint {
    New-PSUApiResponse -StatusCode 404 -Body 'Failed!'
}
```

I lighed med de automatiske fejlkoder vises manuelt returnerede fejlkoder også bedre i PowerShell 7. Her er et eksempel på kald af slutpunktet.

```powershell
PS C:\Users\adamr\Desktop> invoke-restmethod http://localhost:5000/broken

Invoke-RestMethod: Failed!
```

Hvis den kaldes fra Windows PowerShell, modtager De en fejl, der ligner den, der returneres automatisk.

```powershell
PS C:\Users\adamr> invoke-restmethod http://localhost:5000/broken
invoke-restmethod : The remote server returned an error: (404) Not Found.
At line:1 char:1
+ invoke-restmethod http://localhost:5000/broken
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], Web
   Exception
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
```

De kan vælge at returnere fejlkoder, hvis bestemte betingelser er opfyldt, ved at bruge Deres PowerShell-script i slutpunktet.

```powershell
New-PSUEndpoint -Url /user/:name -Endpoint {
    if ($Name -eq 'User')
    {
        @{ UserName = "Adam" }
    }
    else
    {
        New-PSUApiResponse -StatusCode 404 -Body 'Unknown user!'    
    }

}
```

## API

* [New-PSUEndpoint](/powershell-universal/da/powershell-kommandoer/new-psuendpoint.md)
* [Get-PSUEndpoint](/powershell-universal/da/powershell-kommandoer/get-psuendpoint.md)
* [Remove-PSUEndpoint](/powershell-universal/da/powershell-kommandoer/remove-psuendpoint.md)
* [New-PSUApiResponse](/powershell-universal/da/powershell-kommandoer/new-psuapiresponse.md)
* [Set-PSUSetting](/powershell-universal/da/powershell-kommandoer/set-psusetting.md)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.devolutions.net/powershell-universal/da/api/error-handling.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
