<!--
{
  "availability" : [
    "iOS: 8.0.0 -",
    "iPadOS: 8.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.10.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Swift",
  "identifier" : "/documentation/Swift/Result/mapError(_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:s6ResultOsRi_zRi0_zrlE8mapErroryAByxqd__Gqd__q_XEs0C0Rd__lF"
  },
  "title" : "mapError(_:)"
}
-->

# mapError(_:)

Returns a new result, mapping any failure value using the given
transformation.

```
consuming func mapError<NewFailure>(_ transform: (Failure) -> NewFailure) -> Result<Success, NewFailure> where NewFailure : Error
```

## Parameters

`transform`

A closure that takes the failure value of the
instance.

## Return Value

A `Result` instance with the result of evaluating `transform`
as the new failure value if this instance represents a failure.

## Discussion

Use this method when you need to transform the value of a `Result`
instance when it represents a failure. The following example transforms
the error value of a result by wrapping it in a custom `Error` type:

```
struct DatedError: Error {
    var error: Error
    var date: Date

    init(_ error: Error) {
        self.error = error
        self.date = Date()
    }
}

let result: Result<Int, Error> = // ...
// result == .failure(<error value>)
let resultWithDatedError = result.mapError { DatedError($0) }
// result == .failure(DatedError(error: <error value>, date: <date>))
```

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)