<commit_before>//
//  LocalUserReportableError.swift
//  SwiftPlusPlus
//
//  Created by <NAME> on 4/24/16.
//  Copyright © 2016 Drewag LLC. All rights reserved.
//

public struct LocalUserReportableError: UserReportableError {
    public enum Reason {
        case `internal`
        case user
    }

    let source: String
    let operation: String
    let message: String
    let reason: Reason
    public let otherInfo: [String : String]? = nil

    public init(source: String, operation: String, message: String, reason: Reason) {
        self.source = source
        self.operation = operation
        self.message = message
        self.reason = reason
    }

    public init(source: String, operation: String, error: Error, reason: Reason = .internal) {
        if let error = error as? LocalUserReportableError {
            self.source = error.source
            self.operation = error.operation
            self.message = error.message
            self.reason = error.reason
        }
        else {
            self.source = source
            self.operation = operation
            self.message = "\(error)"
            self.reason = reason
        }
    }

    public var alertTitle: String {
        switch self.reason {
        case .user:
            return "Problem \(self.operation)"
        case .internal:
            return "Internal Error"
        }
    }

    public var alertMessage: String {
        switch self.reason {
        case .internal:
            return "Please try again. If the problem persists please contact support with the following description: \(self.message)"
        case .user:
            return message
        }
    }
}<commit_msg>[NF] Added extension to Error to create a user reportable error from it<commit_after>//
//  LocalUserReportableError.swift
//  SwiftPlusPlus
//
//  Created by <NAME> on 4/24/16.
//  Copyright © 2016 Drewag LLC. All rights reserved.
//

public struct LocalUserReportableError: UserReportableError {
    public enum Reason {
        case `internal`
        case user
    }

    let source: String
    let operation: String
    let message: String
    let reason: Reason
    public let otherInfo: [String : String]? = nil

    public init(source: String, operation: String, message: String, reason: Reason) {
        self.source = source
        self.operation = operation
        self.message = message
        self.reason = reason
    }

    public init(source: String, operation: String, error: Error, reason: Reason = .internal) {
        if let error = error as? LocalUserReportableError {
            self.source = error.source
            self.operation = error.operation
            self.message = error.message
            self.reason = error.reason
        }
        else {
            self.source = source
            self.operation = operation
            self.message = "\(error)"
            self.reason = reason
        }
    }

    public var alertTitle: String {
        switch self.reason {
        case .user:
            return "Problem \(self.operation)"
        case .internal:
            return "Internal Error"
        }
    }

    public var alertMessage: String {
        switch self.reason {
        case .internal:
            return "Please try again. If the problem persists please contact support with the following description: \(self.message)"
        case .user:
            return message
        }
    }
}

extension Error {
    public func userReportable(from source: String, for operation: String) -> LocalUserReportableError {
        return LocalUserReportableError(source: source, operation: operation, error: self)
    }
}
{'fraction_non_alphanumeric': 0.08344770793302224, 'fraction_numerical': 0.004940982706560527, 'mean_word_length': 1.9674267100977199, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3383776', 'n_tokens_mistral': 955, 'n_tokens_neox': 909, 'n_words': 308}