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

Not using try! #26

Merged
merged 1 commit into from
Jul 21, 2017
Merged

Not using try! #26

merged 1 commit into from
Jul 21, 2017

Conversation

tapwork
Copy link
Contributor

@tapwork tapwork commented Jul 20, 2017

Use a proper do catch or use try? in combo with guard or if let
and display the error to get more context in failure case.

Use do catch or use try? in combo with guard or if let
@loudmouth
Copy link
Contributor

Hey @tapwork thanks for bringing this issue up and also for getting a fix together!

The try! issue has been in the back of my mind since I picked up this project, but as the persistence library is simply conforming to a protocol that the main SDK defines, I was unsure about how to bubble up thrown errors back through the SDK and to the client program. The messages to the PersistenceIntegration are obviously sent in an async callback from the network and I'd prefer to not require that every network callback throws as it will add unnecessary boilerplate for these callbacks in 90% of cases (i.e. when the consumer is not using a persistence integration).

I think the assertionFailure() is a good solution, but I'm curious if all fatalError() should be swapped for the assertionFailure() calls so that production applications won't crash...I'm not sure if it's preferred to crash the application on failure to create/update a CoreData entity.

@loudmouth
Copy link
Contributor

Just in case my previous comment was unclear, I just wanted to hear your opinion on assertionFailure() vs fatalError() before merging.

@tapwork
Copy link
Contributor Author

tapwork commented Jul 20, 2017

hey @loudmouth
sorry for my late answer.
I also thought about fatalError vs. assertionFailure.
It should be this: fatalError only if the program can't really go on and assertionFailure if the program still could go on. I will update my PR. give 1-2 hours

Copy link
Contributor Author

@tapwork tapwork left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@loudmouth I didn't change the code. I think it might be okayish as it used to be. Can you have a look into my comments

persistable = try self.persistentStore.create(type: type)
persistable.id = asset.id
} catch let error {
fatalError("Could not create the Asset persistent store\n \(error)")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would leave fatalError here (so even crash in live world). There is no guarantee that the program will work properly without the model. (which is expected to exist). The creation of the model should only fail in really rare cases. If it fails something else is really broken.
@loudmouth what do you think?

persistable.id = entry.id
persistable.createdAt = entry.sys.createdAt
} catch let error {
fatalError("Could not create the Entry persistent store\n \(error)")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do {
try persistentStore.save()
} catch let error {
assertionFailure("Could not save the persistent store\n \(error)")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The program can live without a save

@@ -216,16 +227,19 @@ public class SynchronizationManager: PersistenceIntegration {

// Returns regular (non-relationship) field to property mappings.
internal func propertyMapping(for entryType: EntryPersistable.Type, and fields: [FieldName: Any]) -> [FieldName: String] {
// Filter out user-defined properties that represent relationships.
guard let persistentRelationshipPropertyNames = try? persistentStore.relationships(for: entryType) else {
assertionFailure("Could not filter out user-defined properties that represent relationships.")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not 100% sure if this is really necessary to filter out the items in order to have a stable running app.
@loudmouth what do you think?

let spacePersistable: SyncSpacePersistable = try self.persistentStore.create(type: self.persistenceModel.spaceType)
return spacePersistable
} catch let error {
fatalError("Could not create the Sync Space persistent store\n \(error)")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the space is needed for the whole model, right @loudmouth ?

@loudmouth loudmouth merged commit 9244106 into contentful:master Jul 21, 2017
@loudmouth
Copy link
Contributor

I think your approach is correct 👍

Just wanted to think about it. I'm going to merge now. Thanks again for the contribution! I'll add you to the changelog.

@loudmouth
Copy link
Contributor

@tapwork I will probably not be publishing a new version for the next few days, so you will want to point to master in the meantime with your dependency manager.

@tapwork
Copy link
Contributor Author

tapwork commented Jul 21, 2017

thanks @loudmouth
I will take master

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants