Skip to content

Commit

Permalink
Apply review comments by coderabbitai
Browse files Browse the repository at this point in the history
  • Loading branch information
hiddenviewer committed Mar 6, 2025
1 parent 6e26b4b commit 6afc014
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 17 deletions.
11 changes: 10 additions & 1 deletion Sources/Document/Json/JSONTree.swift
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,16 @@ public class JSONTree {
/**
* `toJSON` returns the JSON string of this tree.
*/
public func toJSON() throws -> String {
public func toJSON() -> String {
do {
return try self.toJSONThrows()
} catch {
Logger.critical(String(describing: error))
return ""
}
}

private func toJSONThrows() throws -> String {
guard self.context != nil, let tree else {
throw YorkieError(code: .errNotInitialized, message: "\(type(of: self)) is not initialized yet")
}
Expand Down
1 change: 1 addition & 0 deletions Sources/Document/Operation/TreeEditOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ struct TreeEditOperation: Operation {
public func execute(root: CRDTRoot) throws -> [any OperationInfo] {
guard let parentObject = root.find(createdAt: self.parentCreatedAt) else {
let log = "fail to find \(self.parentCreatedAt)"
Logger.critical(log)
throw YorkieError(code: .errInvalidArgument, message: log)
}

Expand Down
1 change: 1 addition & 0 deletions Sources/Document/Operation/TreeSytleOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class TreeStyleOperation: Operation {
public func execute(root: CRDTRoot) throws -> [any OperationInfo] {
guard let parentObject = root.find(createdAt: self.parentCreatedAt) else {
let log = "fail to find \(self.parentCreatedAt)"
Logger.critical(log)
throw YorkieError(code: .errInvalidArgument, message: log)
}

Expand Down
35 changes: 19 additions & 16 deletions Sources/Util/Errors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
import Connect
import Foundation

/**
* `YorkieError` is an error returned by a Yorkie operation.
*/
struct YorkieError: Error, CustomStringConvertible {
let code: Code
let message: String
Expand All @@ -26,52 +29,52 @@ struct YorkieError: Error, CustomStringConvertible {
}

enum Code: String {
// Ok is returned when the operation completed successfully.
/// Ok is returned when the operation completed successfully.
case ok

// ErrClientNotActivated is returned when the client is not active.
/// ErrClientNotActivated is returned when the client is not active.
case errClientNotActivated = "ErrClientNotActivated"

// ErrClientNotFound is returned when the client is not found.
/// ErrClientNotFound is returned when the client is not found.
case errClientNotFound = "ErrClientNotFound"

// ErrUnimplemented is returned when the operation is not implemented.
/// ErrUnimplemented is returned when the operation is not implemented.
case errUnimplemented = "ErrUnimplemented"

// ErrInvalidType is returned when the type is invalid.
/// ErrInvalidType is returned when the type is invalid.
case errInvalidType = "ErrInvalidType"

// ErrDummy is used to verify errors for testing purposes.
/// ErrDummy is used to verify errors for testing purposes.
case errDummy = "ErrDummy"

// ErrDocumentNotAttached is returned when the document is not attached.
/// ErrDocumentNotAttached is returned when the document is not attached.
case errDocumentNotAttached = "ErrDocumentNotAttached"

// ErrDocumentNotDetached is returned when the document is not detached.
/// ErrDocumentNotDetached is returned when the document is not detached.
case errDocumentNotDetached = "ErrDocumentNotDetached"

// ErrDocumentRemoved is returned when the document is removed.
/// ErrDocumentRemoved is returned when the document is removed.
case errDocumentRemoved = "ErrDocumentRemoved"

// InvalidObjectKey is returned when the object key is invalid.
/// InvalidObjectKey is returned when the object key is invalid.
case errInvalidObjectKey = "ErrInvalidObjectKey"

// ErrInvalidArgument is returned when the argument is invalid.
/// ErrInvalidArgument is returned when the argument is invalid.
case errInvalidArgument = "ErrInvalidArgument"

// ErrNotInitialized is returned when required initialization has not been completed.
/// ErrNotInitialized is returned when required initialization has not been completed.
case errNotInitialized = "ErrNotInitialized"

// ErrNotReady is returned when execution of following actions is not ready.
/// ErrNotReady is returned when execution of following actions is not ready.
case errNotReady = "ErrNotReady"

// ErrRefused is returned when the execution is rejected.
/// ErrRefused is returned when the execution is rejected.
case errRefused = "ErrRefused"

// ErrUnexpected is returned when an unexpected error occurred (iOS only)
/// ErrUnexpected is returned when an unexpected error occurred (iOS only)
case errUnexpected = "ErrUnexpected"

// ErrRPC is returned when an error occurred in the RPC Request
/// ErrRPC is returned when an error occurred in the RPC Request
case errRPC = "ErrRPC"
}
}
Expand Down

0 comments on commit 6afc014

Please sign in to comment.