Skip to content

Commit

Permalink
RUMM-1113 PR comments addressed
Browse files Browse the repository at this point in the history
  • Loading branch information
buranmert committed Feb 25, 2021
1 parent 1dc279d commit 01a6f60
Show file tree
Hide file tree
Showing 17 changed files with 46 additions and 47 deletions.
12 changes: 6 additions & 6 deletions Sources/Datadog/Core/Utils/DDError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,25 @@ import Foundation

/// Common representation of Swift `Error` used by different features.
internal struct DDError: Equatable {
let title: String
let type: String
let message: String
let details: String
let stack: String

init(error: Error) {
if isNSErrorOrItsSubclass(error) {
let nsError = error as NSError
self.title = "\(nsError.domain) - \(nsError.code)"
self.type = "\(nsError.domain) - \(nsError.code)"
if nsError.userInfo[NSLocalizedDescriptionKey] != nil {
self.message = nsError.localizedDescription
} else {
self.message = nsError.description
}
self.details = "\(nsError)"
self.stack = "\(nsError)"
} else {
let swiftError = error
self.title = "\(type(of: swiftError))"
self.type = "\(Swift.type(of: swiftError))"
self.message = "\(swiftError)"
self.details = "\(swiftError)"
self.stack = "\(swiftError)"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ internal struct LoggingWithRUMErrorsIntegration {
func addError(for log: Log) {
rumErrorsIntegration.addError(
with: log.error?.message ?? log.message,
type: log.error?.title,
stack: log.error?.details,
type: log.error?.type,
stack: log.error?.stack,
source: .logger
)
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Datadog/Logger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ public class Logger {
logOutput.writeLogWith(
level: level,
message: message,
error: error,
error: error.flatMap { DDError(error: $0) },
date: date,
attributes: LogAttributes(
userAttributes: combinedUserAttributes,
Expand Down
4 changes: 2 additions & 2 deletions Sources/Datadog/Logging/Log/LogBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ internal struct LogBuilder {
/// Adjusts log's time (device time) to server time.
let dateCorrector: DateCorrectorType?

func createLogWith(level: LogLevel, message: String, error: Error?, date: Date, attributes: LogAttributes, tags: Set<String>) -> Log {
func createLogWith(level: LogLevel, message: String, error: DDError?, date: Date, attributes: LogAttributes, tags: Set<String>) -> Log {
return Log(
date: dateCorrector?.currentCorrection.applying(to: date) ?? date,
status: logStatus(for: level),
message: message,
error: error.flatMap { DDError(error: $0) },
error: error,
serviceName: serviceName,
environment: environment,
loggerName: loggerName,
Expand Down
4 changes: 2 additions & 2 deletions Sources/Datadog/Logging/Log/LogEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ internal struct LogEncoder {

// Encode log.error properties
if let someError = log.error {
try container.encode(someError.title, forKey: .errorKind)
try container.encode(someError.type, forKey: .errorKind)
try container.encode(someError.message, forKey: .errorMessage)
try container.encode(someError.details, forKey: .errorStack)
try container.encode(someError.stack, forKey: .errorStack)
}

// Encode logger info
Expand Down
2 changes: 1 addition & 1 deletion Sources/Datadog/Logging/LogOutputs/LogConsoleOutput.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ internal struct LogConsoleOutput: LogOutput {
self.printingFunction = printingFunction
}

func writeLogWith(level: LogLevel, message: String, error: Error?, date: Date, attributes: LogAttributes, tags: Set<String>) {
func writeLogWith(level: LogLevel, message: String, error: DDError?, date: Date, attributes: LogAttributes, tags: Set<String>) {
let log = logBuilder.createLogWith(level: level, message: message, error: error, date: date, attributes: attributes, tags: tags)
printingFunction(formatter.format(log: log))
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Datadog/Logging/LogOutputs/LogFileOutput.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal struct LogFileOutput: LogOutput {
/// Integration with RUM Errors.
let rumErrorsIntegration: LoggingWithRUMErrorsIntegration?

func writeLogWith(level: LogLevel, message: String, error: Error?, date: Date, attributes: LogAttributes, tags: Set<String>) {
func writeLogWith(level: LogLevel, message: String, error: DDError?, date: Date, attributes: LogAttributes, tags: Set<String>) {
let log = logBuilder.createLogWith(level: level, message: message, error: error, date: date, attributes: attributes, tags: tags)
fileWriter.write(value: log)

Expand Down
2 changes: 1 addition & 1 deletion Sources/Datadog/Logging/LogOutputs/LogOutput.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ internal struct LogAttributes {

/// Type writting logs to some destination.
internal protocol LogOutput {
func writeLogWith(level: LogLevel, message: String, error: Error?, date: Date, attributes: LogAttributes, tags: Set<String>)
func writeLogWith(level: LogLevel, message: String, error: DDError?, date: Date, attributes: LogAttributes, tags: Set<String>)
}
6 changes: 3 additions & 3 deletions Sources/Datadog/Logging/LogOutputs/LogUtilityOutputs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Foundation

/// `LogOutput` which does nothing.
internal struct NoOpLogOutput: LogOutput {
func writeLogWith(level: LogLevel, message: String, error: Error?, date: Date, attributes: LogAttributes, tags: Set<String>) {}
func writeLogWith(level: LogLevel, message: String, error: DDError?, date: Date, attributes: LogAttributes, tags: Set<String>) {}
}

/// Combines one or more `LogOutputs` into one.
Expand All @@ -19,7 +19,7 @@ internal struct CombinedLogOutput: LogOutput {
self.combinedOutputs = outputs
}

func writeLogWith(level: LogLevel, message: String, error: Error?, date: Date, attributes: LogAttributes, tags: Set<String>) {
func writeLogWith(level: LogLevel, message: String, error: DDError?, date: Date, attributes: LogAttributes, tags: Set<String>) {
combinedOutputs.forEach { $0.writeLogWith(level: level, message: message, error: error, date: date, attributes: attributes, tags: tags) }
}
}
Expand All @@ -28,7 +28,7 @@ internal struct ConditionalLogOutput: LogOutput {
let conditionedOutput: LogOutput
let condition: (LogLevel) -> Bool

func writeLogWith(level: LogLevel, message: String, error: Error?, date: Date, attributes: LogAttributes, tags: Set<String>) {
func writeLogWith(level: LogLevel, message: String, error: DDError?, date: Date, attributes: LogAttributes, tags: Set<String>) {
if condition(level) {
conditionedOutput.writeLogWith(level: level, message: message, error: error, date: date, attributes: attributes, tags: tags)
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/Datadog/OpenTracing/OTSpan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ public extension OTSpan {
) {
let dderror = DDError(error: error)
setError(
kind: dderror.title,
kind: dderror.type,
message: dderror.message,
stack: dderror.details,
stack: dderror.stack,
file: file,
line: line
)
Expand Down
8 changes: 4 additions & 4 deletions Sources/Datadog/RUM/RUMMonitor/RUMCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ internal struct RUMAddCurrentViewErrorCommand: RUMCommand {

let dderror = DDError(error: error)
self.message = dderror.message
self.type = dderror.title
self.stack = dderror.details
self.type = dderror.type
self.stack = dderror.stack
}
}

Expand Down Expand Up @@ -217,9 +217,9 @@ internal struct RUMStopResourceWithErrorCommand: RUMResourceCommand {

let dderror = DDError(error: error)
self.errorMessage = dderror.message
self.errorType = dderror.title
self.errorType = dderror.type
// The stack will give the networking error (`NSError`) description in most cases:
self.stack = dderror.details
self.stack = dderror.stack
}
}

Expand Down
20 changes: 10 additions & 10 deletions Tests/DatadogTests/Datadog/Core/Utils/DDErrorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ class DDErrorTests: XCTestCase {
let error = SwiftError()
let dderror = DDError(error: error)

XCTAssertEqual(dderror.title, "SwiftError")
XCTAssertEqual(dderror.type, "SwiftError")
XCTAssertEqual(dderror.message, #"SwiftError(description: "error description")"#)
XCTAssertEqual(dderror.details, #"SwiftError(description: "error description")"#)
XCTAssertEqual(dderror.stack, #"SwiftError(description: "error description")"#)
}

func testFormattingStringConvertibleSwiftError() {
Expand All @@ -29,9 +29,9 @@ class DDErrorTests: XCTestCase {
let error = SwiftError()
let dderror = DDError(error: error)

XCTAssertEqual(dderror.title, "SwiftError")
XCTAssertEqual(dderror.type, "SwiftError")
XCTAssertEqual(dderror.message, "error description")
XCTAssertEqual(dderror.details, "error description")
XCTAssertEqual(dderror.stack, "error description")
}

func testFormattingNSError() {
Expand All @@ -44,10 +44,10 @@ class DDErrorTests: XCTestCase {
)
let dderror = DDError(error: error)

XCTAssertEqual(dderror.title, "custom-domain - 10")
XCTAssertEqual(dderror.type, "custom-domain - 10")
XCTAssertEqual(dderror.message, "error description")
XCTAssertEqual(
dderror.details,
dderror.stack,
"""
Error Domain=custom-domain Code=10 "error description" UserInfo={NSLocalizedDescription=error description}
"""
Expand All @@ -65,10 +65,10 @@ class DDErrorTests: XCTestCase {
)
)

XCTAssertEqual(dderrorWithDescription.title, "custom-domain - 10")
XCTAssertEqual(dderrorWithDescription.type, "custom-domain - 10")
XCTAssertEqual(dderrorWithDescription.message, "localized description")
XCTAssertEqual(
dderrorWithDescription.details,
dderrorWithDescription.stack,
"""
Error Domain=custom-domain Code=10 "localized description" UserInfo={NSLocalizedDescription=localized description}
"""
Expand All @@ -82,13 +82,13 @@ class DDErrorTests: XCTestCase {
)
)

XCTAssertEqual(dderrorNoDescription.title, "custom-domain - 10")
XCTAssertEqual(dderrorNoDescription.type, "custom-domain - 10")
XCTAssertEqual(
dderrorNoDescription.message,
#"Error Domain=custom-domain Code=10 "(null)""#
)
XCTAssertEqual(
dderrorNoDescription.details,
dderrorNoDescription.stack,
#"Error Domain=custom-domain Code=10 "(null)""#
)
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/DatadogTests/Datadog/Logging/Log/LogBuilderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class LogBuilderTests: XCTestCase {
serviceName: "test-service-name",
loggerName: "test-logger-name"
)
let error = ErrorMock("description")
let error = DDError(error: ErrorMock("description"))
let log = builder.createLogWith(
level: .debug,
message: "debug message",
Expand All @@ -28,7 +28,7 @@ class LogBuilderTests: XCTestCase {
XCTAssertEqual(log.applicationVersion, "1.2.3")
XCTAssertEqual(log.status, .debug)
XCTAssertEqual(log.message, "debug message")
XCTAssertEqual(log.error, DDError(error: error))
XCTAssertEqual(log.error, error)
XCTAssertEqual(log.serviceName, "test-service-name")
XCTAssertEqual(log.loggerName, "test-logger-name")
XCTAssertEqual(log.tags, ["tag"])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import XCTest

// swiftlint:disable multiline_arguments_brackets trailing_closure
class LogConsoleOutputTests: XCTestCase {
private let error = ErrorMock("description")
private let error = DDError(error: ErrorMock("description"))

func testItPrintsLogsUsingShortFormat() {
var messagePrinted: String = ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ class CombinedLogOutputTests: XCTestCase {
let output2 = LogOutputMock()
let output3 = LogOutputMock()

let error = ErrorMock("desc")
let ddError = DDError(error: error)
let error = DDError(error: ErrorMock("desc"))
let combinedOutput = CombinedLogOutput(combine: [output1, output2, output3])
combinedOutput.writeLogWith(level: .info, message: "info message", error: error, date: .mockDecember15th2019At10AMUTC(), attributes: .mockAny(), tags: [])

XCTAssertEqual(output1.recordedLog, .init(level: .info, message: "info message", error: ddError, date: .mockDecember15th2019At10AMUTC()))
XCTAssertEqual(output2.recordedLog, .init(level: .info, message: "info message", error: ddError, date: .mockDecember15th2019At10AMUTC()))
XCTAssertEqual(output3.recordedLog, .init(level: .info, message: "info message", error: ddError, date: .mockDecember15th2019At10AMUTC()))
XCTAssertEqual(output1.recordedLog, .init(level: .info, message: "info message", error: error, date: .mockDecember15th2019At10AMUTC()))
XCTAssertEqual(output2.recordedLog, .init(level: .info, message: "info message", error: error, date: .mockDecember15th2019At10AMUTC()))
XCTAssertEqual(output3.recordedLog, .init(level: .info, message: "info message", error: error, date: .mockDecember15th2019At10AMUTC()))
}

func testConditionalLogOutput_writesLogToCombinedOutputOnlyIfConditionIsMet() {
Expand Down
4 changes: 2 additions & 2 deletions Tests/DatadogTests/Datadog/Mocks/LoggingFeatureMocks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,11 @@ class LogOutputMock: LogOutput {

var recordedLog: RecordedLog? = nil

func writeLogWith(level: LogLevel, message: String, error: Error?, date: Date, attributes: LogAttributes, tags: Set<String>) {
func writeLogWith(level: LogLevel, message: String, error: DDError?, date: Date, attributes: LogAttributes, tags: Set<String>) {
recordedLog = RecordedLog(
level: level,
message: message,
error: error.flatMap { DDError(error: $0) },
error: error,
date: date,
attributes: attributes,
tags: tags
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,10 @@ class URLSessionRUMResourcesHandlerTests: XCTestCase {
XCTAssertEqual(resourceStopCommand.resourceKey, taskInterception.identifier.uuidString)
XCTAssertEqual(resourceStopCommand.time, .mockDecember15th2019At10AMUTC())
XCTAssertEqual(resourceStopCommand.attributes.count, 0)
XCTAssertEqual(resourceStopCommand.errorType, DDError(error: taskError).title)
XCTAssertEqual(resourceStopCommand.errorType, DDError(error: taskError).type)
XCTAssertEqual(resourceStopCommand.errorMessage, DDError(error: taskError).message)
XCTAssertEqual(resourceStopCommand.errorSource, .network)
XCTAssertEqual(resourceStopCommand.stack, DDError(error: taskError).details)
XCTAssertEqual(resourceStopCommand.stack, DDError(error: taskError).stack)
XCTAssertNil(resourceStopCommand.httpStatusCode)
}
}

0 comments on commit 01a6f60

Please sign in to comment.