Skip to content

Commit

Permalink
WebErrorKit initial upload
Browse files Browse the repository at this point in the history
  • Loading branch information
rafiki270 committed Jul 17, 2019
0 parents commit 183170b
Show file tree
Hide file tree
Showing 12 changed files with 208 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.DS_Store
.swiftpm
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Einstore

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
24 changes: 24 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// swift-tools-version:5.1
import PackageDescription

let package = Package(
name: "WebError",
products: [
.library(name: "WebErrorKit", targets: ["WebErrorKit"])
],
dependencies: [],
targets: [
.target(
name: "WebErrorKit",
dependencies: []
),
.testTarget(
name: "WebErrorKitTests",
dependencies: [
"WebErrorKit"
]
)
]
)


19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# WebErrorKit



### Install using SPM

```swift
.package(url: "https://github.com/Einstore/WebErrorKit.git", from: "0.0.1")
```

> Discover more commands under `shell.cmd` and `shell.cmd.install`
### Author

Ondrej Rafaj @rafiki270

### License

MIT; Copyright 2019 - Einstore
1 change: 1 addition & 0 deletions Sources/WebErrorKit/Exports.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@_exported import Foundation
14 changes: 14 additions & 0 deletions Sources/WebErrorKit/Extensions/String+Codify.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
extension String {

public func snake_cased() -> String? {
let pattern = "([a-z0-9])([A-Z])"

let regex = try? NSRegularExpression(pattern: pattern, options: [])
let range = NSRange(location: 0, length: count)
return regex?
.stringByReplacingMatches(in: self, options: [], range: range, withTemplate: "$1_$2")
.replacingOccurrences(of: " ", with: "_")
.lowercased()
}

}
10 changes: 10 additions & 0 deletions Sources/WebErrorKit/Extensions/WebError+RawRepresentable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
extension WebError where Self: RawRepresentable, RawValue == String {

/// Default code
public var code: String {
let enumName = String(describing: type(of: self))
let value = rawValue
return "\(enumName.snake_cased() ?? enumName).\(value.snake_cased() ?? value)"
}

}
9 changes: 9 additions & 0 deletions Sources/WebErrorKit/Extensions/WebError+Serializable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
extension SerializableWebError {

/// Default code
public var code: String {
let c = serializedCode
return "\(snake_type).\(c.snake_cased() ?? c)"
}

}
7 changes: 7 additions & 0 deletions Sources/WebErrorKit/SerializableWebError.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/// Serializable WebError for complex errors
public protocol SerializableWebError: WebError {

/// Serialized error code
var serializedCode: String { get }

}
36 changes: 36 additions & 0 deletions Sources/WebErrorKit/WebError.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/// Web error
public protocol WebError: Error {

/// Error status code (default 500)
var statusCode: Int { get }

/// Error code
var code: String { get }

/// Reason for failure (defalt nil)
var reason: String? { get }

}

extension WebError {

/// Default status code
public var statusCode: Int {
return 500
}

/// Default reason
public var reason: String? {
return localizedDescription
}

/// Snake cased Type
public var snake_type: String {
let name = String(describing: type(of: self))
guard let snake = name.snake_cased() else {
return name
}
return snake
}

}
18 changes: 18 additions & 0 deletions Sources/WebErrorKit/WebErrorContent.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/// Codable error content object
public struct WebErrorContent: Codable {

/// Error code
public let code: String

/// Reason for failure
public let reason: String?

/// Initializer
/// - Parameter code: Error code
/// - Parameter reason: Reason for failure
public init(code: String, reason: String? = nil) {
self.code = code
self.reason = reason
}

}
47 changes: 47 additions & 0 deletions Tests/WebErrorKitTests/Tests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import XCTest
import WebErrorKit


enum MyError: String, WebError {
case single
case camelCase
case snake_case
}

enum MyComplexError: SerializableWebError {

case itsComplicated(complication: String)

var serializedCode: String {
switch self {
case .itsComplicated(complication: let c):
return "its_complicated:\(c)"
}
}

}


class Tests: XCTestCase {

func testSingle() {
let err = MyError.single.code
XCTAssertEqual(err, "my_error.single")
}

func testCamelCase() {
let err = MyError.camelCase.code
XCTAssertEqual(err, "my_error.camel_case")
}

func testSnakeCase() {
let err = MyError.snake_case.code
XCTAssertEqual(err, "my_error.snake_case")
}

func testComplicatedCase() {
let err = MyComplexError.itsComplicated(complication: "Huge problem").code
XCTAssertEqual(err, "my_complex_error.its_complicated:huge_problem")
}

}

0 comments on commit 183170b

Please sign in to comment.