Files
purchases-ios/Sources/FoundationExtensions/Error+Extensions.swift
T
NachoSoto ea7230de38 NetworkError.errorResponse: added status code to userInfo (#1480)
This could help debug these errors.

Thanks to the error refactor this only had to be put in one place.
2022-04-12 15:14:15 -07:00

48 lines
1.6 KiB
Swift

//
// Copyright RevenueCat Inc. All Rights Reserved.
//
// Licensed under the MIT License (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://opensource.org/licenses/MIT
//
// Error+Extensions.swift
//
// Created by Joshua Liebowitz on 8/6/21.
import Foundation
extension Error {
/**
* Addes a sub-error to the userInfo of a new `error` object as some extra context. Sometimes we have multiple error
* Conditions but only a single place to surface them. This adds the second error as extra context to help during
* debugging.
* - Returns: a new error matching `self` but with the `extraContext` and `error` added.
*/
func addingUnderlyingError(_ error: Error?, extraContext: String? = nil) -> Error {
guard let underlyingNSError = error as NSError? else {
return self
}
let asNSError = self as NSError
var userInfo = asNSError.userInfo as [NSError.UserInfoKey: Any]
userInfo[NSUnderlyingErrorKey as NSString] = underlyingNSError
userInfo[.extraContext] = extraContext ?? underlyingNSError.localizedDescription
let nsErrorWithUserInfo = NSError(domain: asNSError.domain,
code: asNSError.code,
userInfo: userInfo as [String: Any])
return nsErrorWithUserInfo as Error
}
}
extension NSError {
var subscriberAttributesErrors: [String: String]? {
return self.userInfo[ErrorDetails.attributeErrorsKey] as? [String: String]
}
}