Skip to content

Commit

Permalink
kintsugi: Handle expected errors gracefully.
Browse files Browse the repository at this point in the history
Instead of printing the stack trace, just print the errors.
  • Loading branch information
byohay committed Dec 16, 2021
1 parent aa174c6 commit b949db5
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 26 deletions.
10 changes: 9 additions & 1 deletion bin/kintsugi
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

require "kintsugi"
require_relative "../lib/kintsugi/cli"
require_relative "../lib/kintsugi/error"

def parse_options!(command, argv)
options = {}
Expand All @@ -28,4 +29,11 @@ command =
end

options = parse_options!(command, ARGV)
command.action.call(options, ARGV)

begin
command.action.call(options, ARGV)
rescue ArgumentError => e
puts "#{e.class}: #{e}"
rescue Kintsugi::MergeError => e
puts e
end
13 changes: 8 additions & 5 deletions lib/kintsugi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

require_relative "kintsugi/xcodeproj_extensions"
require_relative "kintsugi/apply_change_to_project"
require_relative "kintsugi/error"

module Kintsugi
class << self
Expand All @@ -23,9 +24,11 @@ class << self
# @raise [ArgumentError]
# If the file extension is not `pbxproj` or the file doesn't exist
#
# @raise [RuntimeError]
# If no rebase, cherry-pick, or merge is in progress, or the project file couldn't be
# opened, or there was an error applying the change to the project.
# @raise [EnvironmentError]
# If no rebase, cherry-pick, or merge is in progress.
#
# @raise [MergeError]
# If there was an error applying the change to the project.
#
# @return [void]
def resolve_conflicts(project_file_path, changes_output_path)
Expand Down Expand Up @@ -104,8 +107,8 @@ def validate_project(project_file_path)

Dir.chdir(File.dirname(project_file_path)) do
unless file_has_base_ours_and_theirs_versions?(project_file_path)
raise ArgumentError, "File '#{project_file_path}' doesn't have conflicts, or a 3-way " \
"merge is not possible."
raise ArgumentError, "File '#{project_file_path}' doesn't have conflicts, " \
"or a 3-way merge is not possible."
end
end
end
Expand Down
42 changes: 22 additions & 20 deletions lib/kintsugi/apply_change_to_project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,24 +190,24 @@ def apply_removal_to_simple_attribute(old_value, removed_change, added_change)
when Hash
(old_value || {}).reject do |key, value|
if value != removed_change[key] && added_change[key] != value
raise "Trying to remove value '#{removed_change[key]}' of hash with key '#{key}' but " \
"it changed to #{value}. This is considered a conflict that should be resolved " \
"manually."
raise MergeError, "Trying to remove value '#{removed_change[key]}' of hash with key " \
"'#{key}' but it changed to #{value}. This is considered a conflict that should be " \
"resolved manually."
end

removed_change.key?(key)
end
when String
if old_value != removed_change && !old_value.nil? && added_change != old_value
raise "Trying to remove value '#{removed_change}', but the existing value is " \
"'#{old_value}'. This is considered a conflict that should be resolved manually."
raise MergeError, "Trying to remove value '#{removed_change}', but the existing value " \
"is '#{old_value}'. This is considered a conflict that should be resolved manually."
end

nil
when nil
nil
else
raise "Unsupported change #{removed_change} of type #{removed_change.class}"
raise MergeError, "Unsupported change #{removed_change} of type #{removed_change.class}"
end
end

Expand All @@ -220,7 +220,8 @@ def apply_addition_to_simple_attribute(old_value, change)
new_value = old_value.merge(change)

unless (old_value.to_a - new_value.to_a).empty?
raise "New hash #{change} contains values that conflict with old hash #{old_value}"
raise MergeError, "New hash #{change} contains values that conflict with old hash " \
"#{old_value}"
end

new_value
Expand All @@ -229,14 +230,15 @@ def apply_addition_to_simple_attribute(old_value, change)
when nil
nil
else
raise "Unsupported change #{change} of type #{change.class}"
raise MergeError, "Unsupported change #{change} of type #{change.class}"
end
end

def remove_component(component, change)
if component.to_tree_hash != change
raise "Trying to remove an object that changed since then. This is considered a conflict " \
"that should be resolved manually. Name of the object is: '#{component.display_name}'"
raise MergeError, "Trying to remove an object that changed since then. This is " \
"considered a conflict that should be resolved manually. Name of the object is: " \
"'#{component.display_name}'"
end

if change["isa"] == "PBXFileReference"
Expand Down Expand Up @@ -302,8 +304,8 @@ def add_child_to_component(component, change)
when "PBXReferenceProxy"
add_reference_proxy(component, change)
else
raise "Trying to add unsupported component type #{change["isa"]}. Full component change " \
"is: #{change}"
raise MergeError, "Trying to add unsupported component type #{change["isa"]}. Full " \
"component change is: #{change}"
end
end

Expand Down Expand Up @@ -331,7 +333,7 @@ def add_reference_proxy(containing_component, change)
containing_component << reference_proxy
add_attributes_to_component(reference_proxy, change)
else
raise "Trying to add reference proxy to an unsupported component type " \
raise MergeError, "Trying to add reference proxy to an unsupported component type " \
"#{containing_component.isa}. Change is: #{change}"
end
end
Expand All @@ -346,7 +348,7 @@ def add_variant_group(containing_component, change)
containing_component.children << variant_group
add_attributes_to_component(variant_group, change)
else
raise "Trying to add variant group to an unsupported component type " \
raise MergeError, "Trying to add variant group to an unsupported component type " \
"#{containing_component.isa}. Change is: #{change}"
end
end
Expand Down Expand Up @@ -442,7 +444,7 @@ def add_container_item_proxy(component, change)
when "PBXReferenceProxy"
component.remote_ref = container_proxy
else
raise "Trying to add container item proxy to an unsupported component type " \
raise MergeError, "Trying to add container item proxy to an unsupported component type " \
"#{containing_component.isa}. Change is: #{change}"
end
add_attributes_to_component(container_proxy, change, ignore_keys: ["containerPortal"])
Expand Down Expand Up @@ -541,7 +543,7 @@ def add_file_reference(containing_component, change)
file_reference.include_in_index = nil
add_attributes_to_component(file_reference, change)
else
raise "Trying to add file reference to an unsupported component type " \
raise MergeError, "Trying to add file reference to an unsupported component type " \
"#{containing_component.isa}. Change is: #{change}"
end
end
Expand All @@ -556,8 +558,8 @@ def add_group(containing_component, change)
new_group = containing_component.project.new(Xcodeproj::Project::PBXGroup)
containing_component.children << new_group
else
raise "Trying to add group to an unsupported component type #{containing_component.isa}. " \
"Change is: #{change}"
raise MergeError, "Trying to add group to an unsupported component type " \
"#{containing_component.isa}. Change is: #{change}"
end

add_attributes_to_component(new_group, change)
Expand All @@ -581,8 +583,8 @@ def add_attributes_to_component(component, change, ignore_keys: [])
add_child_to_component(component, added_attribute_element)
end
else
raise "Trying to add attribute of unsupported type '#{change_value.class}' to " \
"object #{component}. Attribute name is '#{change_name}'"
raise MergeError, "Trying to add attribute of unsupported type '#{change_value.class}' " \
"to object #{component}. Attribute name is '#{change_name}'"
end
end
end
Expand Down
9 changes: 9 additions & 0 deletions lib/kintsugi/error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Copyright (c) 2021 Lightricks. All rights reserved.
# Created by Ben Yohay.
# frozen_string_literal: true

module Kintsugi
# Raised when an error occurred while Kintsugi tried to resolve conflicts.
class MergeError < RuntimeError
end
end

0 comments on commit b949db5

Please sign in to comment.