Inheritance from non-protocol type 'Error' in Swift Package Manager

Hi.. i am trying to create Custom errors in swift . i have created sample cmd project . When i am throwing error customer error from main.swift file of cmd project . it is working fine .

struct CustomError: Error {
    enum ErrorKind {
        case invalidCharacter
        case mismatchedTag
        case internalError
    }
    let code: Int
    let message: String
    let kind: ErrorKind
}

Working fine

public func parse() throws -> String {
    // ...
    throw CustomError(code: 19, message: "Message",kind: .invalidCharacter)
    // ...
}

now , I have created my own swift package and added that package to cmd project . I have to throw custom error from class i have created in swift package.When i am trying to use the same error structure in package manager i am getting this error :
Error :

Inheritance from non-protocol type 'Error' 

Code In swift package module:

struct CustomError: Error {
    enum ErrorKind {
        case invalidCharacter
        case mismatchedTag
        case internalError
    }
    let code: Int
    let message: String
    let kind: ErrorKind
}

public class test {
    var hello : String = ""
    public init(_hello: String) throws {
        throw CustomError(code: 19, message: "Message",kind: .invalidCharacter)
    }
}

Question :
How can resolve this error in swift package manager . throwing custom error seems to be working fine without swift package.

You almost certainly have a type somewhere in the same module, or imported from a different module, called Error. Change struct CustomError: Error to struct CustomError: Swift.Error and this should be fixed.

3 Likes