- $id = $notice->insert();
-
- if (!$id) {
- common_log_db_error($notice, 'INSERT', __FILE__);
- // TRANS: Server exception thrown when a notice cannot be saved.
- throw new ServerException(_('Problem saving notice.'));
- }
-
- // Update ID-dependent columns: URI, conversation
-
- $orig = clone($notice);
-
- $changed = false;
-
- // We can only get here if it's a local notice, since remote notices
- // should've bailed out earlier due to lacking a URI.
- if (empty($notice->uri)) {
- $notice->uri = sprintf('%s%s=%d:%s=%s',
- TagURI::mint(),
- 'noticeId', $notice->id,
- 'objectType', $notice->get_object_type(true));
- $changed = true;
- }
-
- // If it's not part of a conversation, it's
- // the beginning of a new conversation.
-
- if (empty($notice->conversation)) {
- $conv = Conversation::create($notice);
- $notice->conversation = $conv->id;
- $changed = true;
- }
-
- if ($changed) {
- if ($notice->update($orig) === false) {
- common_log_db_error($notice, 'UPDATE', __FILE__);
- // TRANS: Server exception thrown when a notice cannot be updated.
- throw new ServerException(_('Problem saving notice.'));
+ try {
+ $notice->insert(); // throws exception on failure
+ // If it's not part of a conversation, it's
+ // the beginning of a new conversation.
+ if (empty($notice->conversation)) {
+ $orig = clone($notice);
+ // $act->context->conversation will be null if it was not provided
+ $conv = Conversation::create($notice, $options['conversation']);
+ $notice->conversation = $conv->id;
+ $notice->update($orig);
+ }
+ } catch (Exception $e) {
+ // Let's test if we managed initial insert, which would imply
+ // failing on some update-part (check 'insert()'). Delete if
+ // something had been stored to the database.
+ if (!empty($notice->id)) {
+ $notice->delete();