Providing a unified Swift error API
John Sundell

May be I’d do this way

enum Result<T> { //can do Error as generic too
case  success(T)
case error(SearchError)
func loadSearchData(matching query: String) -> Result<Data> {
let urlString = "\(query)"

guard let url = URL(string: urlString) else {
return .error(SearchError.invalidQuery(query))
  do {
return .success(try Data(contentsOf: url))
} catch {
    return .fail(SearchError.dataLoadingFailed(url))
From this point you know exactly what type of Error to expect when calling this method.