CoreStoreError not handling Core Data validation errors #71

Closed
opened 2025-12-29 15:23:35 +01:00 by adam · 2 comments
Owner

Originally created by @jannon on GitHub (Jul 21, 2016).

Hi, so I ran across some bad server data of mine that violated a validation rule on a field in one of my entities and I only get an "Unknown" error from CoreStore

The field has a max length validation rule and the data pulled from a server went over that limit. Stepping through the code in the debugger, I get to NSManagedObjectContext+transaction.swift:174

Inspecting error I find it is an NSError with domain = "NSCocoaErrorDomain" and code = 1660
and it has a userInfo with the following key/value pairs:

  • "NSValidationErrorObject": <The object I attempted to save>
  • "NSValidationErrorKey": "summary",
  • "NSLocalizedDescription": "summary is too long",
  • "NSValidationErrorValue": "<The contents of the summary that are too long>"

The CoreStoreError that gets initialized from that is Unknown and so is completely useless to the calling code.

I'm not sure how self = error.flatMap { $0.bridgeToSwift } ?? .Unknown is supposed to work in the CoreStoreError.init, but it seems to be missing something.

Bottom line is, it would be great if validation errors (and any other reasonable Core Data errors) were bubbled up through CoreStoreError so they could be handled properly in the UI

Originally created by @jannon on GitHub (Jul 21, 2016). Hi, so I ran across some bad server data of mine that violated a validation rule on a field in one of my entities and I only get an "Unknown" error from CoreStore The field has a max length validation rule and the data pulled from a server went over that limit. Stepping through the code in the debugger, I get to [NSManagedObjectContext+transaction.swift:174](https://github.com/JohnEstropia/CoreStore/blob/develop/Sources/Internal/NSManagedObjectContext%2BTransaction.swift#L174) Inspecting `error` I find it is an `NSError` with `domain` = "NSCocoaErrorDomain" and `code` = 1660 and it has a userInfo with the following key/value pairs: - "NSValidationErrorObject": &lt;The object I attempted to save&gt; - "NSValidationErrorKey": "summary", - "NSLocalizedDescription": "summary is too long", - "NSValidationErrorValue": "&lt;The contents of the summary that are too long&gt;" The CoreStoreError that gets initialized from that is `Unknown` and so is completely useless to the calling code. I'm not sure how `self = error.flatMap { $0.bridgeToSwift } ?? .Unknown` is supposed to work in the `CoreStoreError.init`, but it seems to be missing something. Bottom line is, it would be great if validation errors (and any other reasonable Core Data errors) were bubbled up through `CoreStoreError` so they could be handled properly in the UI
adam added the fixedcorestore bug labels 2025-12-29 15:23:35 +01:00
adam closed this issue 2025-12-29 15:23:35 +01:00
Author
Owner

@JohnEstropia commented on GitHub (Jul 21, 2016):

Thanks for the report! This is a bug and not the intended design. Internal errors are supposed to fall back to CoreStoreError.InternalError and not .Unknown.

I fixed this in 2.0.2 and updated the unit tests. Once the Travis CI checks complete I'll update the master branch.

Once updated, internal NSErrors should all be wrapped around CoreStoreError.InternalError. To inspect the error details just do a print(error) and you should be given something like

(CoreStore.CoreStoreError) .InternalError (
    ._domain = "com.corestore.error";
    ._code = 4;
    .NSError = (
        .domain = "com.dummy";
        .code = 123;
        .userInfo = 3 key-value(s) [
            key3 = 2016-07-21 02:38:55 +0000;
            key1 = value1;
            key2 = 2;
        ];
    );
)
@JohnEstropia commented on GitHub (Jul 21, 2016): Thanks for the report! This is a bug and not the intended design. Internal errors are supposed to fall back to `CoreStoreError.InternalError` and not `.Unknown`. I fixed this in `2.0.2` and updated the unit tests. Once the Travis CI checks complete I'll update the master branch. Once updated, internal NSErrors should all be wrapped around `CoreStoreError.InternalError`. To inspect the error details just do a `print(error)` and you should be given something like ``` (CoreStore.CoreStoreError) .InternalError ( ._domain = "com.corestore.error"; ._code = 4; .NSError = ( .domain = "com.dummy"; .code = 123; .userInfo = 3 key-value(s) [ key3 = 2016-07-21 02:38:55 +0000; key1 = value1; key2 = 2; ]; ); ) ```
Author
Owner

@jannon commented on GitHub (Jul 22, 2016):

awesome!

@jannon commented on GitHub (Jul 22, 2016): awesome!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore#71