Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/jsonapi errors #69

Closed
wants to merge 11 commits into from
38 changes: 37 additions & 1 deletion Kakapo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@

/* Begin PBXBuildFile section */
5B16AF891A00E1CEB873C5F8 /* Pods_Kakapo_iOSTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1250C40FD2117CD20E6AE31F /* Pods_Kakapo_iOSTests.framework */; };
8BE0FC801D16BD9A00FE706A /* JSONAPILinksTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8BE0FC7E1D16BD8300FE706A /* JSONAPILinksTests.swift */; };
8BE0FC811D16BD9B00FE706A /* JSONAPILinksTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8BE0FC7E1D16BD8300FE706A /* JSONAPILinksTests.swift */; };
8BE0FC821D16BD9C00FE706A /* JSONAPILinksTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8BE0FC7E1D16BD8300FE706A /* JSONAPILinksTests.swift */; };
8BE0FC841D172EC900FE706A /* JSONAPILinks.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8BE0FC831D172EC900FE706A /* JSONAPILinks.swift */; };
8BE0FC851D172EC900FE706A /* JSONAPILinks.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8BE0FC831D172EC900FE706A /* JSONAPILinks.swift */; };
8BE0FC861D172EC900FE706A /* JSONAPILinks.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8BE0FC831D172EC900FE706A /* JSONAPILinks.swift */; };
8BE0FC871D172EC900FE706A /* JSONAPILinks.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8BE0FC831D172EC900FE706A /* JSONAPILinks.swift */; };
AC9577F342F6730725CC9D72 /* Pods_Kakapo_tvOSTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C67882333142806D5CF9A918 /* Pods_Kakapo_tvOSTests.framework */; };
D5363674ABF8735A0A1368BD /* Pods_Kakapo_macOSTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = ABE4F11D5A141DB87CD81023 /* Pods_Kakapo_macOSTests.framework */; };
DE76E1311D0DC62B009721A4 /* Kakapo.h in Headers */ = {isa = PBXBuildFile; fileRef = DE76E0FA1D0DC37E009721A4 /* Kakapo.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand Down Expand Up @@ -74,6 +81,13 @@
DE76E1CD1D0DC857009721A4 /* XCTestCase+CustomAssertions.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE76E1B71D0DC857009721A4 /* XCTestCase+CustomAssertions.swift */; };
DE76E1CE1D0DC857009721A4 /* XCTestCase+CustomAssertions.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE76E1B71D0DC857009721A4 /* XCTestCase+CustomAssertions.swift */; };
DE76E1CF1D0DC857009721A4 /* XCTestCase+CustomAssertions.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE76E1B71D0DC857009721A4 /* XCTestCase+CustomAssertions.swift */; };
DE82027A1D20068A00552FEC /* JSONAPIError.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE8202791D20068A00552FEC /* JSONAPIError.swift */; };
DE82027B1D20068A00552FEC /* JSONAPIError.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE8202791D20068A00552FEC /* JSONAPIError.swift */; };
DE82027C1D20068A00552FEC /* JSONAPIError.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE8202791D20068A00552FEC /* JSONAPIError.swift */; };
DE82027D1D20068A00552FEC /* JSONAPIError.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE8202791D20068A00552FEC /* JSONAPIError.swift */; };
DE82027F1D200C9000552FEC /* JSONAPIErrorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE82027E1D200C9000552FEC /* JSONAPIErrorTests.swift */; };
DE8202801D200C9000552FEC /* JSONAPIErrorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE82027E1D200C9000552FEC /* JSONAPIErrorTests.swift */; };
DE8202811D200C9000552FEC /* JSONAPIErrorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE82027E1D200C9000552FEC /* JSONAPIErrorTests.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -103,6 +117,8 @@
/* Begin PBXFileReference section */
1250C40FD2117CD20E6AE31F /* Pods_Kakapo_iOSTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Kakapo_iOSTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
822EBBB73B6B4117D229CDF1 /* Pods-Kakapo tvOSTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Kakapo tvOSTests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-Kakapo tvOSTests/Pods-Kakapo tvOSTests.debug.xcconfig"; sourceTree = "<group>"; };
8BE0FC7E1D16BD8300FE706A /* JSONAPILinksTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = JSONAPILinksTests.swift; sourceTree = "<group>"; };
8BE0FC831D172EC900FE706A /* JSONAPILinks.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = JSONAPILinks.swift; sourceTree = "<group>"; };
ABE4F11D5A141DB87CD81023 /* Pods_Kakapo_macOSTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Kakapo_macOSTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
C67882333142806D5CF9A918 /* Pods_Kakapo_tvOSTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Kakapo_tvOSTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
C9E9EAD776E83C0F5785A0DB /* Pods-Kakapo macOSTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Kakapo macOSTests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-Kakapo macOSTests/Pods-Kakapo macOSTests.debug.xcconfig"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -132,6 +148,8 @@
DE76E1B51D0DC857009721A4 /* SerializerTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SerializerTests.swift; sourceTree = "<group>"; };
DE76E1B61D0DC857009721A4 /* URLDecomposerTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = URLDecomposerTests.swift; sourceTree = "<group>"; };
DE76E1B71D0DC857009721A4 /* XCTestCase+CustomAssertions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "XCTestCase+CustomAssertions.swift"; sourceTree = "<group>"; };
DE8202791D20068A00552FEC /* JSONAPIError.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = JSONAPIError.swift; sourceTree = "<group>"; };
DE82027E1D200C9000552FEC /* JSONAPIErrorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = JSONAPIErrorTests.swift; sourceTree = "<group>"; };
DEBD3C741D16BC9A004E0A23 /* README.playground */ = {isa = PBXFileReference; lastKnownFileType = file.playground; path = README.playground; sourceTree = "<group>"; };
DFC29110721D31BE30199824 /* Pods-Kakapo iOSTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Kakapo iOSTests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-Kakapo iOSTests/Pods-Kakapo iOSTests.debug.xcconfig"; sourceTree = "<group>"; };
E357918ACA29146B64834E63 /* Pods-Kakapo tvOSTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Kakapo tvOSTests.release.xcconfig"; path = "Pods/Target Support Files/Pods-Kakapo tvOSTests/Pods-Kakapo tvOSTests.release.xcconfig"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -261,21 +279,25 @@
DE76E0FD1D0DC38B009721A4 /* Tests */ = {
isa = PBXGroup;
children = (
DE76E0FE1D0DC38B009721A4 /* Info.plist */,
8BE0FC7E1D16BD8300FE706A /* JSONAPILinksTests.swift */,
DE76E1B11D0DC857009721A4 /* JSONAPITests.swift */,
DE82027E1D200C9000552FEC /* JSONAPIErrorTests.swift */,
DE76E1B21D0DC857009721A4 /* KakapoDBTests.swift */,
DE76E1B31D0DC857009721A4 /* PropertyPolicyTests.swift */,
DE76E1B41D0DC857009721A4 /* RouterTests.swift */,
DE76E1B51D0DC857009721A4 /* SerializerTests.swift */,
DE76E1B61D0DC857009721A4 /* URLDecomposerTests.swift */,
DE76E1B71D0DC857009721A4 /* XCTestCase+CustomAssertions.swift */,
DE76E0FE1D0DC38B009721A4 /* Info.plist */,
);
path = Tests;
sourceTree = "<group>";
};
DE76E1001D0DC395009721A4 /* Source */ = {
isa = PBXGroup;
children = (
DE8202791D20068A00552FEC /* JSONAPIError.swift */,
8BE0FC831D172EC900FE706A /* JSONAPILinks.swift */,
DE76E1011D0DC395009721A4 /* JSONAPISerializer.swift */,
DE76E1021D0DC395009721A4 /* KakapoDB.swift */,
DE76E1031D0DC395009721A4 /* KakapoServer.swift */,
Expand Down Expand Up @@ -717,9 +739,11 @@
DE76E19A1D0DC755009721A4 /* PropertyPolicy.swift in Sources */,
DE76E19D1D0DC755009721A4 /* URLDecomposer.swift in Sources */,
DE76E1971D0DC755009721A4 /* KakapoDB.swift in Sources */,
8BE0FC851D172EC900FE706A /* JSONAPILinks.swift in Sources */,
DE76E19C1D0DC755009721A4 /* Serializer.swift in Sources */,
DE76E1961D0DC755009721A4 /* JSONAPISerializer.swift in Sources */,
DE76E19B1D0DC755009721A4 /* Router.swift in Sources */,
DE82027B1D20068A00552FEC /* JSONAPIError.swift in Sources */,
DE76E1991D0DC755009721A4 /* NSURLRequest+FixCopy.swift in Sources */,
DE76E1A71D0DC762009721A4 /* CustomAssertions.swift in Sources */,
);
Expand All @@ -733,9 +757,11 @@
DE76E1921D0DC755009721A4 /* PropertyPolicy.swift in Sources */,
DE76E1951D0DC755009721A4 /* URLDecomposer.swift in Sources */,
DE76E18F1D0DC755009721A4 /* KakapoDB.swift in Sources */,
8BE0FC861D172EC900FE706A /* JSONAPILinks.swift in Sources */,
DE76E1941D0DC755009721A4 /* Serializer.swift in Sources */,
DE76E18E1D0DC755009721A4 /* JSONAPISerializer.swift in Sources */,
DE76E1931D0DC755009721A4 /* Router.swift in Sources */,
DE82027C1D20068A00552FEC /* JSONAPIError.swift in Sources */,
DE76E1911D0DC755009721A4 /* NSURLRequest+FixCopy.swift in Sources */,
DE76E1A81D0DC762009721A4 /* CustomAssertions.swift in Sources */,
);
Expand All @@ -746,10 +772,12 @@
buildActionMask = 2147483647;
files = (
DE76E1BC1D0DC857009721A4 /* JSONAPITests.swift in Sources */,
8BE0FC811D16BD9B00FE706A /* JSONAPILinksTests.swift in Sources */,
DE76E1CB1D0DC857009721A4 /* URLDecomposerTests.swift in Sources */,
DE76E1BF1D0DC857009721A4 /* KakapoDBTests.swift in Sources */,
DE76E1C81D0DC857009721A4 /* SerializerTests.swift in Sources */,
DE76E1C51D0DC857009721A4 /* RouterTests.swift in Sources */,
DE8202801D200C9000552FEC /* JSONAPIErrorTests.swift in Sources */,
DE76E1CE1D0DC857009721A4 /* XCTestCase+CustomAssertions.swift in Sources */,
DE76E1C21D0DC857009721A4 /* PropertyPolicyTests.swift in Sources */,
);
Expand All @@ -763,9 +791,11 @@
DE76E18A1D0DC754009721A4 /* PropertyPolicy.swift in Sources */,
DE76E18D1D0DC754009721A4 /* URLDecomposer.swift in Sources */,
DE76E1871D0DC754009721A4 /* KakapoDB.swift in Sources */,
8BE0FC871D172EC900FE706A /* JSONAPILinks.swift in Sources */,
DE76E18C1D0DC754009721A4 /* Serializer.swift in Sources */,
DE76E1861D0DC754009721A4 /* JSONAPISerializer.swift in Sources */,
DE76E18B1D0DC754009721A4 /* Router.swift in Sources */,
DE82027D1D20068A00552FEC /* JSONAPIError.swift in Sources */,
DE76E1891D0DC754009721A4 /* NSURLRequest+FixCopy.swift in Sources */,
DE76E1A91D0DC763009721A4 /* CustomAssertions.swift in Sources */,
);
Expand All @@ -776,10 +806,12 @@
buildActionMask = 2147483647;
files = (
DE76E1BD1D0DC857009721A4 /* JSONAPITests.swift in Sources */,
8BE0FC821D16BD9C00FE706A /* JSONAPILinksTests.swift in Sources */,
DE76E1CC1D0DC857009721A4 /* URLDecomposerTests.swift in Sources */,
DE76E1C01D0DC857009721A4 /* KakapoDBTests.swift in Sources */,
DE76E1C91D0DC857009721A4 /* SerializerTests.swift in Sources */,
DE76E1C61D0DC857009721A4 /* RouterTests.swift in Sources */,
DE8202811D200C9000552FEC /* JSONAPIErrorTests.swift in Sources */,
DE76E1CF1D0DC857009721A4 /* XCTestCase+CustomAssertions.swift in Sources */,
DE76E1C31D0DC857009721A4 /* PropertyPolicyTests.swift in Sources */,
);
Expand All @@ -793,9 +825,11 @@
DE76E1A21D0DC756009721A4 /* PropertyPolicy.swift in Sources */,
DE76E1A51D0DC756009721A4 /* URLDecomposer.swift in Sources */,
DE76E19F1D0DC756009721A4 /* KakapoDB.swift in Sources */,
8BE0FC841D172EC900FE706A /* JSONAPILinks.swift in Sources */,
DE76E1A41D0DC756009721A4 /* Serializer.swift in Sources */,
DE76E19E1D0DC756009721A4 /* JSONAPISerializer.swift in Sources */,
DE76E1A31D0DC756009721A4 /* Router.swift in Sources */,
DE82027A1D20068A00552FEC /* JSONAPIError.swift in Sources */,
DE76E1A11D0DC756009721A4 /* NSURLRequest+FixCopy.swift in Sources */,
DE76E1A61D0DC761009721A4 /* CustomAssertions.swift in Sources */,
);
Expand All @@ -806,10 +840,12 @@
buildActionMask = 2147483647;
files = (
DE76E1BB1D0DC857009721A4 /* JSONAPITests.swift in Sources */,
8BE0FC801D16BD9A00FE706A /* JSONAPILinksTests.swift in Sources */,
DE76E1CA1D0DC857009721A4 /* URLDecomposerTests.swift in Sources */,
DE76E1BE1D0DC857009721A4 /* KakapoDBTests.swift in Sources */,
DE76E1C71D0DC857009721A4 /* SerializerTests.swift in Sources */,
DE76E1C41D0DC857009721A4 /* RouterTests.swift in Sources */,
DE82027F1D200C9000552FEC /* JSONAPIErrorTests.swift in Sources */,
DE76E1CD1D0DC857009721A4 /* XCTestCase+CustomAssertions.swift in Sources */,
DE76E1C11D0DC857009721A4 /* PropertyPolicyTests.swift in Sources */,
);
Expand Down
80 changes: 80 additions & 0 deletions Source/JSONAPIError.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//
// JSONAPIError.swift
// Kakapo
//
// Created by Alex Manzella on 26/06/16.
// Copyright © 2016 devlucky. All rights reserved.
//

import Foundation

// A convenince error object that conform to JSON API
public struct JSONAPIError: ResponseFieldsProvider {

/// A builder for JSONAPIError
public struct JSONAPIErrorBuilder: Serializable {
/// A unique identifier for this particular occurrence of the problem.
public var id: String?

/// A link object that leads to further details about this particular occurrence of the problem.
public var about: JSONAPILink?

/// The HTTP status code applicable to this problem, expressed as a string value.
public var status: Int

/// An application-specific error code, expressed as a string value
public var code: String?

/// A short, human-readable summary of the problem that SHOULD NOT change from occurrence to occurrence of the problem, except for purposes of localization
public var title: String?

/// A human-readable explanation specific to this occurrence of the problem. Like title, this field’s value can be localized.
public var detail: String?

/**
An object containing references to the source of the error, optionally including any of the following members:

- pointer: a JSON `Pointer` ([RFC6901](https://tools.ietf.org/html/rfc6901)) to the associated entity in the request document [e.g. `/data` for a primary data object, or `/data/attributes/title` for a specific attribute].
- parameter: a string indicating which URI query parameter caused the error.
*/
public var source: String?

/// A meta object containing non-standard meta-information about the error.
public var meta: Serializable?

private init(statusCode: Int) {
status = statusCode
}
}

private let builder: JSONAPIErrorBuilder

// MARK: ResponseFieldsProvider

public var statusCode: Int {
return builder.status
}

public var body: Serializable {
return builder
}

public var headerFields: [String : String]? {
return nil
}

/**
Initialize a `JSONAPIError` and build it with `JSONAPIErrorBuilder`

- parameter statusCode: the status code of the response, will be used also to provide a statusCode for your request
- parameter errorBuilder: A builder that can be used to fill the error objects, it contains all you need to provide an error object confiorming to JSON API (**see `JSONAPIErrorBuilder`**)

- returns: An error that conforms to JSON API specifications and it's ready to be serialized
*/
public init(statusCode: Int, errorBuilder: (error: inout JSONAPIErrorBuilder) -> ()) {
var builder = JSONAPIErrorBuilder(statusCode: statusCode)
errorBuilder(error: &builder)
self.builder = builder
}

}
Loading