From b949db55e8dd84e351f9faa0ac40c721293d0749 Mon Sep 17 00:00:00 2001 From: Ben Yohay Date: Wed, 15 Dec 2021 18:11:25 +0200 Subject: [PATCH] kintsugi: Handle expected errors gracefully. Instead of printing the stack trace, just print the errors. --- bin/kintsugi | 10 +++++- lib/kintsugi.rb | 13 +++++--- lib/kintsugi/apply_change_to_project.rb | 42 +++++++++++++------------ lib/kintsugi/error.rb | 9 ++++++ 4 files changed, 48 insertions(+), 26 deletions(-) create mode 100644 lib/kintsugi/error.rb diff --git a/bin/kintsugi b/bin/kintsugi index e2e7e0a..4d36667 100755 --- a/bin/kintsugi +++ b/bin/kintsugi @@ -6,6 +6,7 @@ require "kintsugi" require_relative "../lib/kintsugi/cli" +require_relative "../lib/kintsugi/error" def parse_options!(command, argv) options = {} @@ -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 diff --git a/lib/kintsugi.rb b/lib/kintsugi.rb index 23fa51f..5a2f7aa 100644 --- a/lib/kintsugi.rb +++ b/lib/kintsugi.rb @@ -9,6 +9,7 @@ require_relative "kintsugi/xcodeproj_extensions" require_relative "kintsugi/apply_change_to_project" +require_relative "kintsugi/error" module Kintsugi class << self @@ -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) @@ -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 diff --git a/lib/kintsugi/apply_change_to_project.rb b/lib/kintsugi/apply_change_to_project.rb index 8b4e52f..2ca0a6b 100644 --- a/lib/kintsugi/apply_change_to_project.rb +++ b/lib/kintsugi/apply_change_to_project.rb @@ -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 @@ -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 @@ -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" @@ -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 @@ -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 @@ -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 @@ -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"]) @@ -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 @@ -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) @@ -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 diff --git a/lib/kintsugi/error.rb b/lib/kintsugi/error.rb new file mode 100644 index 0000000..db8dec3 --- /dev/null +++ b/lib/kintsugi/error.rb @@ -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