diff --git a/Sources/JSONAPI/Error/BasicJSONAPIError.swift b/Sources/JSONAPI/Error/BasicJSONAPIError.swift index 345af26..fa0158d 100644 --- a/Sources/JSONAPI/Error/BasicJSONAPIError.swift +++ b/Sources/JSONAPI/Error/BasicJSONAPIError.swift @@ -70,4 +70,9 @@ public struct BasicJSONAPIErrorPayload: Codable, Eq /// a good option if you do not know what to expect. You could also use /// `Either` (provided by the `Poly` package that is /// already a dependency of `JSONAPI`). +/// +/// - Important: The `definedFields` property will include fields +/// with non-nil values in a flattened way. There will be no `source` key +/// but there will be `pointer` and `parameter` keys (if those values +/// are non-nil). public typealias BasicJSONAPIError = GenericJSONAPIError> diff --git a/Tests/JSONAPITests/Error/BasicJSONAPIErrorTests.swift b/Tests/JSONAPITests/Error/BasicJSONAPIErrorTests.swift new file mode 100644 index 0000000..e726143 --- /dev/null +++ b/Tests/JSONAPITests/Error/BasicJSONAPIErrorTests.swift @@ -0,0 +1,92 @@ +// +// BasicJSONAPIErrorTests.swift +// JSONAPITests +// +// Created by Mathew Polzin on 9/29/19. +// + +import Foundation +@testable import JSONAPI +import XCTest + +final class BasicJSONAPIErrorTests: XCTestCase { + func test_initAndEquality() { + let unknown1 = BasicJSONAPIError.unknown + let unknown2 = BasicJSONAPIError.unknownError + XCTAssertEqual(unknown1, unknown2) + let unknown3 = BasicJSONAPIError.unknownError + XCTAssertEqual(unknown3, .unknown) + + let _ = BasicJSONAPIError.error(.init(id: nil, + status: nil, + code: nil, + title: nil, + detail: nil, + source: nil)) + let _ = BasicJSONAPIError.error(.init(id: nil, + status: nil, + code: nil, + title: nil, + detail: nil, + source: nil)) + + let intError = BasicJSONAPIError.error(.init(id: 2, + status: nil, + code: nil, + title: nil, + detail: nil, + source: nil)) + XCTAssertEqual(intError.payload?.id, 2) + XCTAssertNotEqual(intError, unknown3) + + let stringError = BasicJSONAPIError.error(.init(id: "hello", + status: nil, + code: nil, + title: nil, + detail: nil, + source: nil)) + XCTAssertEqual(stringError.payload?.id, "hello") + XCTAssertNotEqual(stringError, unknown1) + + let wellPopulatedError = BasicJSONAPIError.error(.init(id: 10, + status: "404", + code: "12", + title: "Missing", + detail: "Resource was not found", + source: .init(pointer: "/data/attributes/id", parameter: "id"))) + XCTAssertEqual(wellPopulatedError.payload?.id, 10) + XCTAssertEqual(wellPopulatedError.payload?.status, "404") + XCTAssertEqual(wellPopulatedError.payload?.code, "12") + XCTAssertEqual(wellPopulatedError.payload?.title, "Missing") + XCTAssertEqual(wellPopulatedError.payload?.detail, "Resource was not found") + XCTAssertEqual(wellPopulatedError.payload?.source?.pointer, "/data/attributes/id") + XCTAssertEqual(wellPopulatedError.payload?.source?.parameter, "id") + + XCTAssertNotEqual(wellPopulatedError, intError) + } + + func test_definedFields() { + let unpopulatedError = BasicJSONAPIError.error(.init(id: nil, + status: nil, + code: nil, + title: nil, + detail: nil, + source: nil)) + XCTAssertEqual(unpopulatedError.definedFields.count, 0) + + let wellPopulatedError = BasicJSONAPIError.error(.init(id: 10, + status: "404", + code: "12", + title: "Missing", + detail: "Resource was not found", + source: .init(pointer: "/data/attributes/id", parameter: "id"))) + XCTAssertEqual(wellPopulatedError.definedFields.count, 7) + XCTAssertEqual(wellPopulatedError.definedFields["id"], "10") + XCTAssertEqual(wellPopulatedError.definedFields["status"], "404") + XCTAssertEqual(wellPopulatedError.definedFields["code"], "12") + XCTAssertEqual(wellPopulatedError.definedFields["title"], "Missing") + XCTAssertEqual(wellPopulatedError.definedFields["detail"], "Resource was not found") + XCTAssertEqual(wellPopulatedError.definedFields["pointer"], "/data/attributes/id") + XCTAssertEqual(wellPopulatedError.definedFields["parameter"], "id") + } +} diff --git a/Tests/JSONAPITests/Error/GenericJSONAPIErrorTests.swift b/Tests/JSONAPITests/Error/GenericJSONAPIErrorTests.swift index e1fca6e..21172ed 100644 --- a/Tests/JSONAPITests/Error/GenericJSONAPIErrorTests.swift +++ b/Tests/JSONAPITests/Error/GenericJSONAPIErrorTests.swift @@ -92,6 +92,14 @@ final class GenericJSONAPIErrorTests: XCTestCase { } } + func test_encodeUnknown() { + let error = TestGenericJSONAPIError.unknownError + + let encodedError = encoded(value: ["errors": [error]]) + + XCTAssertEqual(String(data: encodedError, encoding: .utf8)!, #"{"errors":["unknown"]}"#) + } + func test_payloadAccess() { let error1 = TestGenericJSONAPIError.error(.init(hello: "world", world: 3)) let error2 = TestGenericJSONAPIError.error(.init(hello: "there", world: nil))