> 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/de/api/error-handling.md).

# Fehlerbehandlung

Standardmäßig geben Endpunkte eine 200 OK-Meldung zurück, selbst wenn Fehler auftreten. Tritt ein Fehler auf, erhalten Sie eine leere Antwort vom Endpunkt. Dieses Dokument zeigt verschiedene Möglichkeiten, Fehler innerhalb von APIs zu behandeln.

## Fehler automatisch zurückgeben

Um Fehler automatisch von APIs zurückzugeben, können Sie das Standardverhalten ändern, indem Sie den Parameter `-ErrorAction` von `New-PSUEndpoint` auf `Stop` setzen. Alle Fehler führen dann dazu, dass ein 500 Internal Server Error mit einer Liste der Fehler und dem Stacktrace zurückgegeben wird.

Terminierende Fehler geben immer einen 500 Internal Server Error zurück.

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

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

Sie werden ein unterschiedliches Verhalten in Windows PowerShell und PowerShell 7 feststellen, wenn Sie REST-APIs aufrufen, die Fehler zurückgeben. In Windows PowerShell erhalten Sie einen allgemeinen Fehler, der die Fehlermeldung nicht zurückgibt.

```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
```

In PowerShell 7 wird beim Zurückgeben eines Fehlers die Fehlermeldung angezeigt.

```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
```

Sie können die Fehlermeldung in Windows PowerShell mit der folgenden Syntax abrufen.

```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
```

## Fehler manuell zurückgeben

Um Fehler manuell zurückzugeben, müssen Sie das Cmdlet `New-PSUApiResponse` verwenden. Dieses Cmdlet erlaubt es Ihnen, den Statuscode und den Body für die Antwort zu definieren.

In diesem Beispiel geben wir einen 404-Fehlercode vom Endpunkt zurück.

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

Ähnlich wie die automatischen Fehlercodes werden manuell zurückgegebene Fehlercodes in PowerShell 7 besser dargestellt. Hier ist ein Beispiel für den Aufruf des Endpunkts.

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

Invoke-RestMethod: Failed!
```

Beim Aufruf aus Windows PowerShell erhalten Sie einen Fehler, der dem automatisch zurückgegebenen ähnelt.

```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
```

Sie können festlegen, dass Fehlercodes zurückgegeben werden, wenn bestimmte Bedingungen erfüllt sind, indem Sie Ihr PowerShell-Skript innerhalb des Endpunkts verwenden.

```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/de/powershell-befehle/new-psuendpoint.md)
* [Get-PSUEndpoint](/powershell-universal/de/powershell-befehle/get-psuendpoint.md)
* [Remove-PSUEndpoint](/powershell-universal/de/powershell-befehle/remove-psuendpoint.md)
* [New-PSUApiResponse](/powershell-universal/de/powershell-befehle/new-psuapiresponse.md)
* [Set-PSUSetting](/powershell-universal/de/powershell-befehle/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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
