Codable Failure Response Handler
There are two approaches


For the sake of completeness, I’ll describe the typical, simple solution:

struct UserLoginResponse: Codable {
    let status: Bool
    let error: ErrorResponse?
    let accessToken: String?
    let data: [UserLoginData]?
}


The status is not optional (because it’s presumably there regardless). But you can just decode this struct:

do {
    let responseObject = try decoder.decode(UserLoginResponse.self, from: data)
    switch responseObject.status {
    case true:
        guard let accessToken = responseObject.accessToken, let userLoginData = responseObject.data else {
            throw ParsingError.requiredFieldMissing
        }

        // use accessToken and userLoginData here

    case false:
        guard let errorObject = responseObject.error else {
            throw ParsingError.requiredFieldMissing
        }

        // use errorObject here
    }
} catch {
    print(error)
}

If you really want to do this generic, wrapper approach, I’d suggest a slight refinement. Notably, the code in your question returns an object for which both the success and error objects are optionals. (There seem like there are a lot of ? thrown in there quite liberally, whereas most API dictate “if success, x and y will be present, if failure, z will be present”.)

I’d suggest, instead, a pattern that captures the fact that the response will be either success (when status is True) or failure (when status is False), and rather than returning both success and failure objects as optionals, use the Result&lt;Success, Failure&gt; enumeration with associated values, which is included in Swift 5. Or, if you’re using an earlier version of Swift, you can define it yourself:

enum Result&lt;Success, Failure&gt; {
    case success(Success)
    case failure(Failure)
}


Then, the API response wrapper can have a non-optional result property of type Result&lt;T, E&gt;, where:


If status is True, parse the success object as a non-optional associated value which is the Success type;
If status is False, parse the error object as a non-optional associated value which is the Failure type;&nbsp;


Thus:

struct ApiResponse&lt;T: Codable, E: Codable&gt;: Codable {
    let status: Bool
    var result: Result&lt;T, E&gt;
    var error: E?

    enum CodingKeys: String, CodingKey {
        case status, error
    }

    init(from decoder: Decoder) throws {
        let values = try decoder.container(keyedBy: CodingKeys.self)
        status = try values.decode(Bool.self, forKey: .status)
        if status {
            let singleValue = try decoder.singleValueContainer()
            result = try .success(singleValue.decode(T.self))
        } else {
            let parsedError = try .failure(values.decode(E.self, forKey: .error))
            error = parsedError
            result = .failure(parsedError)
        }
    }
}


Then you can do:

do {
    let responseObject = try decoder.decode(ApiResponse&lt;UserLoginResponse, ErrorResponse&gt;.self, from: data)
    switch responseObject.result {
    case .success(let object):
        print(object.accessToken, object.data)

    case .failure(let error):
        print(error)
    }
} catch {
    print(error)
}


This would seem to better capture the true nature of the response, that it’s either successful (and you get the non-optional success object) or it’s a failure (and you get the non-optional ErrorResponse object).

By the way, I’d suggest that in this scenario, that UserLoginResponse be updated to reflect which fields are truly optional and which aren’t. For example, if you know that if status is True, that both accessToken and data will be present, then I’d make those non-optional properties:

struct UserLoginResponse: Codable {
    let accessToken: String
    let data: [UserLoginData]
}