---
title: "replaceError(with:)"
framework: combine
role: symbol
role_heading: Instance Method
path: "combine/publisher/replaceerror(with:)"
---

# replaceError(with:)

Replaces any errors in the stream with the provided element.

## Declaration

```swift
func replaceError(with output: Self.Output) -> Publishers.ReplaceError<Self>
```

## Parameters

- `output`: An element to emit when the upstream publisher fails.

## Return Value

Return Value A publisher that replaces an error from the upstream publisher with the provided output element.

## Discussion

Discussion If the upstream publisher fails with an error, this publisher emits the provided element, then finishes normally. In the example below, a publisher of strings fails with a MyError instance, which sends a failure completion downstream. The replaceError(with:) operator handles the failure by publishing the string (replacement element) and completing normally. struct MyError: Error {} let fail = Fail<String, MyError>(error: MyError()) cancellable = fail     .replaceError(with: "(replacement element)")     .sink(         receiveCompletion: { print ("\($0)") },         receiveValue: { print ("\($0)", terminator: " ") }     )

// Prints: "(replacement element) finished". This replaceError(with:) functionality is useful when you want to handle an error by sending a single replacement element and end the stream. Use catch(_:) to recover from an error and provide a replacement publisher to continue providing elements to the downstream subscriber.

## See Also

### Filtering elements

- [filter(_:)](combine/publisher/filter(_:).md)
- [tryFilter(_:)](combine/publisher/tryfilter(_:).md)
- [compactMap(_:)](combine/publisher/compactmap(_:).md)
- [tryCompactMap(_:)](combine/publisher/trycompactmap(_:).md)
- [removeDuplicates()](combine/publisher/removeduplicates().md)
- [removeDuplicates(by:)](combine/publisher/removeduplicates(by:).md)
- [tryRemoveDuplicates(by:)](combine/publisher/tryremoveduplicates(by:).md)
- [replaceEmpty(with:)](combine/publisher/replaceempty(with:).md)
