Skip to content

Commit

Permalink
adds button to test 500 error reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
smirolo committed Oct 4, 2024
1 parent 9cd4e1a commit cc4f4b5
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 23 deletions.
40 changes: 24 additions & 16 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@

APP_NAME ?= $(notdir $(abspath $(srcDir)))
APP_PORT ?= 8000
LIVEDEMO_ENTRY_POINT ?= http://djaopsp-demo

srcDir ?= .
installTop ?= $(VIRTUAL_ENV)
binDir ?= $(installTop)/bin
libDir ?= $(installTop)/lib
SYSCONFDIR := $(installTop)/etc
LOCALSTATEDIR := $(installTop)/var
CONFIG_DIR := $(SYSCONFDIR)/$(APP_NAME)
ASSETS_DIR := $(srcDir)/htdocs/assets
LIVEDEMO_ENTRY_POINT ?= http://djaopsp-demo
SYSCONFDIR ?= $(installTop)/etc
LOCALSTATEDIR ?= $(installTop)/var
CONFIG_DIR ?= $(SYSCONFDIR)/$(APP_NAME)
ASSETS_DIR ?= $(srcDir)/htdocs/assets
RUN_DIR ?= $(abspath $(srcDir))

installDirs ?= /usr/bin/install -d
installFiles ?= /usr/bin/install -p -m 644
Expand All @@ -23,7 +24,7 @@ ESCHECK ?= eslint
NPM ?= npm
PIP ?= pip
PYTHON ?= python
SASSC ?= sassc --style=compressed
SASSC ?= sassc --style=compressed --source-map-urls absolute
# As of sqlite3 version 3.42.0 (2023-05-16) we need to pass `-unsafe-testing`
# to make adjustments in the demo database.
SQLITE_NO_UNSAFE_TESTING := $(shell echo '.schema' | sqlite3 -unsafe-testing > /dev/null 2>&1; echo $$?)
Expand All @@ -45,13 +46,14 @@ WEBPACK ?= NODE_PATH=$(libDir)/node_modules:$(NODE_PATH) webpack --stats-e
MANAGE := DJAOAPP_SETTINGS_LOCATION=$(CONFIG_DIR) $(PYTHON) manage.py
RUNSYNCDB = $(if $(findstring --run-syncdb,$(shell cd $(srcDir) && $(MANAGE) migrate --help 2>/dev/null)),--run-syncdb,)


ifneq ($(wildcard $(CONFIG_DIR)/site.conf),)
# `make initdb` will install site.conf but only after `grep` is run
# and DB_FILNAME set to "". We use the default value in the template site.conf
# and DB_FILENAME set to "". We use the default value in the template site.conf
# here to prevent that issue.
DB_FILENAME := $(shell grep ^DB_NAME $(CONFIG_DIR)/site.conf | cut -f 2 -d '"')
DB_FILENAME ?= $(shell grep ^DB_NAME $(CONFIG_DIR)/site.conf | cut -f 2 -d '"')
else
DB_FILENAME ?= $(if $(wildcard $(LOCALSTATEDIR)/db/),$(LOCALSTATEDIR)/db/db.sqlite,$(srcDir)/db.sqlite)
DB_FILENAME ?= $(RUN_DIR)/db.sqlite
endif
LIVEDEMO_DB_FILENAME := $(srcDir)/db.sqlite
MULTITIER_DB_FILENAME := $(dir $(DB_FILENAME))cowork.sqlite
Expand All @@ -63,7 +65,7 @@ MY_EMAIL ?= $(shell cd $(srcDir) && git config user.email)
EMAIL_FIXTURE_OPT := $(if $(MY_EMAIL),--email="$(MY_EMAIL)",)


.PHONY: build-assets doc generateschema initdb makemessages package-docker setup-livedemo vendor-assets-prerequisites
.PHONY: build-assets doc generateschema initdb makemessages setup-livedemo vendor-assets-prerequisites

all:
@echo "Nothing to be done for 'make'."
Expand Down Expand Up @@ -120,13 +122,17 @@ makemessages:
cd $(srcDir) && $(MANAGE) makemessages -l fr -l es -l pt --symlinks --no-wrap
cd $(srcDir) && $(MANAGE) makemessages -d djangojs -l fr -l es -l pt --symlinks --no-wrap

# !!! Attention !!!
# You will need to run `make initdb` or `make setup-livedemo` before
# running `package-docker` in order to create the dummy db.sqilte file
# to package.
package-docker: build-assets
ifeq ($(MY_EMAIL),)

.PHONY: package-docker

# We build a local sqlite3 database to be packaged with the Docker image
# such that the container can be started without prior configuration.
package-docker: build-assets initdb
[[ -f $(srcDir)/db.sqlite ]] || cp $(DB_FILENAME) $(srcDir)/db.sqlite
cd $(srcDir) && $(DOCKER) build $(DOCKER_OPTS) .

endif

# download and install prerequisites then create the db.
require: require-pip require-resources initdb
Expand Down Expand Up @@ -364,7 +370,8 @@ $(DESTDIR)$(CONFIG_DIR)/site.conf: $(srcDir)/etc/site.conf
-e 's,%(APP_NAME)s,$(APP_NAME),' \
-e "s,%(ADMIN_EMAIL)s,$(MY_EMAIL)," \
-e 's,%(installTop)s,$(installTop),' \
-e "s,%(DB_FILENAME)s,$(abspath $(DB_FILENAME))," \
-e "s,%(DB_NAME)s,$(APP_NAME)," \
-e "s,%(DB_FILENAME)s,$(DB_FILENAME)," \
-e "s,%(binDir)s,$(binDir)," $< > $@

$(DESTDIR)$(CONFIG_DIR)/credentials: $(srcDir)/etc/credentials
Expand Down Expand Up @@ -404,6 +411,7 @@ $(DESTDIR)$(SYSCONFDIR)/monit.d/%: $(srcDir)/etc/monit.conf
$(installDirs) $(dir $@)
[ -e $@ ] || sed \
-e 's,%(APP_NAME)s,$(APP_NAME),g' \
-e 's,%(APP_PORT)s,$(APP_NAME),g' \
-e 's,%(LOCALSTATEDIR)s,$(LOCALSTATEDIR),' $< > $@

$(DESTDIR)$(SYSCONFDIR)/sysconfig/%: $(srcDir)/etc/sysconfig.conf
Expand Down
10 changes: 8 additions & 2 deletions djaoapp/api/todos.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2021, DjaoDjin inc.
# Copyright (c) 2024, DjaoDjin inc.
# see LICENSE
from __future__ import unicode_literals

Expand All @@ -8,7 +8,7 @@
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import ensure_csrf_cookie
from rest_framework import status
from rest_framework.generics import ListAPIView, RetrieveAPIView
from rest_framework.generics import CreateAPIView, ListAPIView, RetrieveAPIView
from rest_framework.response import Response
from saas.mixins import ProviderMixin
from signup.serializers import ActivitySerializer
Expand Down Expand Up @@ -54,6 +54,12 @@ def get(self, request, *args, **kwargs):
return Response(serializer.data, status=status.HTTP_200_OK)


class GenerateErrorAPIView(ProviderMixin, CreateAPIView):

def create(self, request, *args, **kwargs):
raise RuntimeError("Testing: Generated Error")


def list_todos(request, provider=None):
#pylint:disable=unused-argument
return []
Expand Down
8 changes: 4 additions & 4 deletions djaoapp/templates/rules/app_dashboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -302,16 +302,14 @@ <h3>{% trans %}Testing{% endtrans %}</h3>
</div>

<!-- Triggering 500 errors -->
{% if FEATURES_DEBUG %}
<hr />
<div>
<form method="post" action="/api">
<form method="post" action="{{'/api/proxy/generate-error'|site_url}}">
<input type="hidden" name="csrfmiddlewaretoken" value="{{csrf_token}}">
<input type="hidden" name="username" value="{{request.user.username}}">
<button id="error500" class="btn btn-outline-primary">{% trans %}Trigger 500 Error{% endtrans %}</button>
</form>
</div>
{% endif %}
</div>
</div><!-- /testing -->
</div>
Expand Down Expand Up @@ -344,10 +342,12 @@ <h3>{% trans %}Testing{% endtrans %}</h3>

jQuery(document).ready(function($) {
$("#error500").click(function (evt) {
var self = this;
//const url = self.parents('form').attr('action');
evt.preventDefault();
jQuery.ajax({
method: 'POST',
url: '/api',
url: "{{'/api/proxy/generate-error'|site_url}}",
beforeSend: function(xhr, settings) {
if( !(/^(GET|HEAD|OPTIONS|TRACE)$/.test(settings.type)) ) {
var csrfToken = getCSRFToken();
Expand Down
4 changes: 3 additions & 1 deletion djaoapp/urls/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from ..api.organizations import (DjaoAppProfileDetailAPIView,
DjaoAppProfileListAPIView, DjaoAppProfilePictureAPIView)
from ..api.roles import DjaoAppRoleByDescrListAPIView
from ..api.todos import DjaoAppAPIVersion, TodosAPIView
from ..api.todos import DjaoAppAPIVersion, TodosAPIView, GenerateErrorAPIView
from ..api.users import (RecentActivityAPIView, DjaoAppUserDetailAPIView,
DjaoAppUserNotificationsAPIView, DjaoAppUserOTPAPIView)
from ..urlbuilders import (url_authenticated, url_direct,
Expand All @@ -35,6 +35,8 @@
NotificationAPIView.as_view(), name='api_notification_base'),
url_direct(r'^api/proxy/recent',
RecentActivityAPIView.as_view(), name='api_recent_activity'),
url_direct(r'^api/proxy/generate-error',
GenerateErrorAPIView.as_view(), name='api_generate_error'),
url_direct(r'^api/', include('rules.urls.api.proxy')),
url_direct(r'^api/themes$',
DjaoAppThemePackageListAPIView.as_view(),
Expand Down

0 comments on commit cc4f4b5

Please sign in to comment.