From 77666b3829b0f674e2ce4dbf3dc2f7cfc8bc2bae Mon Sep 17 00:00:00 2001 From: Iain Bryson <iain@iain-bryson.ca> Date: Fri, 12 May 2017 13:25:13 -0500 Subject: [PATCH] =?UTF-8?q?Make=20the=20client=20logging=20more=20robust?= =?UTF-8?q?=20to=20circular=20JSON.=20Add=20a=20helper=20exec=20method=20t?= =?UTF-8?q?o=20the=20git=20service=20to=20enable=20=E2=80=9Craw=E2=80=9D?= =?UTF-8?q?=20use=20of=20Git.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../javascripts/dtu_core/client_debug.js | 6 +++- .../dtu_core/json-stringify-safe.js | 28 +++++++++++++++++++ .../javascripts/dtu_core/report_a_bug.coffee | 7 +++-- lib/git_services.rb | 10 +++++++ 4 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 app/assets/javascripts/dtu_core/json-stringify-safe.js diff --git a/app/assets/javascripts/dtu_core/client_debug.js b/app/assets/javascripts/dtu_core/client_debug.js index 6e2fe15..fa50916 100644 --- a/app/assets/javascripts/dtu_core/client_debug.js +++ b/app/assets/javascripts/dtu_core/client_debug.js @@ -12,10 +12,14 @@ var ClientDebug = { message: message }; + var json = stringifySafe(packet, null, 2) + $.ajax({ method: "PUT", url: ClientDebug.resource_url, - data: JSON.stringify(packet, null, 2) + contentType: "application/json; charset=utf-8", + dataType: 'json', + data: json }) .done(function(data, textStatus, jqXHR) { console.log("client log: success") diff --git a/app/assets/javascripts/dtu_core/json-stringify-safe.js b/app/assets/javascripts/dtu_core/json-stringify-safe.js new file mode 100644 index 0000000..a2f2a6a --- /dev/null +++ b/app/assets/javascripts/dtu_core/json-stringify-safe.js @@ -0,0 +1,28 @@ +// from https://github.com/isaacs/json-stringify-safe/blob/02cfafd45f06d076ac4bf0dd28be6738a07a72f9/stringify.js#L4 +// per http://stackoverflow.com/questions/11616630/json-stringify-avoid-typeerror-converting-circular-structure-to-json +// to avoid circular refs error when logging mathjax errors + +function stringifySafe(obj, replacer, spaces, cycleReplacer) { + return JSON.stringify(obj, serializerSafe(replacer, cycleReplacer), spaces) +} + +function serializerSafe(replacer, cycleReplacer) { + var stack = [], + keys = [] + + if (cycleReplacer == null) cycleReplacer = function(key, value) { + if (stack[0] === value) return "[Circular ~]" + return "[Circular ~." + keys.slice(0, stack.indexOf(value)).join(".") + "]" + } + + return function(key, value) { + if (stack.length > 0) { + var thisPos = stack.indexOf(this); + ~thisPos ? stack.splice(thisPos + 1) : stack.push(this); + ~thisPos ? keys.splice(thisPos, Infinity, key) : keys.push(key); + if (~stack.indexOf(value)) value = cycleReplacer.call(this, key, value); + } else stack.push(value) + + return replacer == null ? value : replacer.call(this, key, value) + } +} \ No newline at end of file diff --git a/app/assets/javascripts/dtu_core/report_a_bug.coffee b/app/assets/javascripts/dtu_core/report_a_bug.coffee index b1f5c1a..6165864 100644 --- a/app/assets/javascripts/dtu_core/report_a_bug.coffee +++ b/app/assets/javascripts/dtu_core/report_a_bug.coffee @@ -19,9 +19,10 @@ window.onerror = (msg, url, lineNo, columnNo, error) -> do -> kinds = ['log', 'warn', 'error', 'info'] + window._CONSOLE = {} for kind in kinds - oldLog = console[kind] - if typeof oldLog is 'function' + window._CONSOLE[kind] = console[kind] + if typeof window._CONSOLE is 'function' console[kind] = (message) -> ConsoleBuffer.log({kind: kind, message: message}) - oldLog.apply(console, arguments) + _CONSOLE[kind].apply(console, arguments) diff --git a/lib/git_services.rb b/lib/git_services.rb index b48f8c6..398aa6f 100644 --- a/lib/git_services.rb +++ b/lib/git_services.rb @@ -153,6 +153,16 @@ module DtuCore true end + def exec() + begin + g = Git.open(self.git_repo_info.local_repo_folder, :log => Rails.logger) + set_git_identity(g) + yield g + rescue => e + Rails.logger.warn "Unable to change, will ignore: #{e}" + end + end + def set_git_identity(g) g.config('user.name', (self.git_user.full_name || 'aonymous') + ' [from DTU quiz]') g.config('user.email', self.git_user.email) -- GitLab