Skip to content

Commit

Permalink
Merge pull request #26 from johanneswilm/master
Browse files Browse the repository at this point in the history
use fetch api when available & fix demo
  • Loading branch information
Natim authored Oct 17, 2022
2 parents 27ccd8e + 2bee6a0 commit 16ac743
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 65 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ node_modules/
package.json
package-lock.json
.tox/
build/
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ Then install the urls::
In your template, simply add the js_error_hook script::

<script type="text/javascript">
var djangoJSErrorHandlerUrl = "{% url 'js-error-handler' %}";
window.djangoJSErrorHandlerUrl = "{% url 'js-error-handler' %}"
</script>
<script type="text/javascript" defer src="{% static 'django_js_error_hook/utils.js' %}"></script>
<script type="text/javascript" src="{% static 'js/django_js_error_hook.js' %}"></script>

Now every JavaScript error will be logged in your logging error stream. (Mail, Sentry, ...)

Expand Down
6 changes: 5 additions & 1 deletion demo/demoproject/templates/error_test.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
Expand All @@ -6,7 +7,10 @@
<meta name="description" content="Novasupport">
<meta name="author" content="Jojax">
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<script type="text/javascript" src="{% url 'js-error-handler-js' %}"></script>
<script type="text/javascript">
window.djangoJSErrorHandlerUrl = "{% url 'js-error-handler' %}"
</script>
<script type="text/javascript" src="{% static 'js/django_js_error_hook.js' %}"></script>
</head>

<body>
Expand Down
3 changes: 1 addition & 2 deletions demo/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ def read_relative_file(filename):
classifiers=[
"Development Status :: 1 - Planning",
"License :: OSI Approved :: BSD License",
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 2.6",
"Programming Language :: Python :: 3",
"Framework :: Django",
],
keywords="class-based view, generic view, js error hooking",
Expand Down
60 changes: 0 additions & 60 deletions django_js_error_hook/static/django_js_error_hook/utils.js

This file was deleted.

82 changes: 82 additions & 0 deletions django_js_error_hook/static/js/django_js_error_hook.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
(function () {
function getCookie (name) {
if (!document.cookie || document.cookie === '') {
return null
}
const cookie = document.cookie.split(';').map(cookie => cookie.trim()).find(cookie => {
if (cookie.substring(0, name.length + 1) === (name + '=')) {
return true
} else {
return false
}
})
if (cookie) {
return decodeURIComponent(cookie.substring(name.length + 1))
}
return null
}
function logError (details) {
const cookie = getCookie('csrftoken')
const data = {
context: String(navigator.userAgent),
details: String(details)
}

if (window.fetch) {
const body = new FormData()
body.append('context', data.context)
body.append('details', data.details)

return fetch(window.djangoJSErrorHandlerUrl, {
method: 'POST',
headers: {
'X-CSRFToken': cookie
},
credentials: 'include',
body
})
} else {
const xhr = new XMLHttpRequest()

xhr.open('POST', window.djangoJSErrorHandlerUrl, true)
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded')

if (cookie) {
xhr.setRequestHeader('X-CSRFToken', cookie)
}
const query = []
for (const key in data) {
query.push(encodeURIComponent(key) + '=' + encodeURIComponent(data[key]))
}
xhr.send(query.join('&'))
}
}

window.onerror = function (msg, url, lineNumber, columnNumber, errorObj) {
let logMessage = url + ': ' + lineNumber + ': ' + msg
if (columnNumber) {
logMessage += ', ' + columnNumber
}
if (errorObj && errorObj.stack) {
logMessage += ', ' + errorObj.stack
}
logError(logMessage)
}

if (window.addEventListener) {
window.addEventListener('unhandledrejection', function (rejection) {
let logMessage = rejection.type
if (rejection.reason) {
if (rejection.reason.message) {
logMessage += ', ' + rejection.reason.message
} else {
logMessage += ', ' + JSON.stringify(rejection.reason)
}
if (rejection.reason.stack) {
logMessage += ', ' + rejection.reason.stack
}
}
logError(logMessage)
})
}
})()

0 comments on commit 16ac743

Please sign in to comment.