Include link to support in error message #149
3 changed files with 92 additions and 27 deletions
|
@ -10,6 +10,7 @@ from django.core.exceptions import ValidationError
|
||||||
from django.db import IntegrityError, models, transaction
|
from django.db import IntegrityError, models, transaction
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from django.utils.functional import cached_property
|
from django.utils.functional import cached_property
|
||||||
|
from django.utils.safestring import mark_safe
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
from encrypted_fields.fields import EncryptedJSONField
|
from encrypted_fields.fields import EncryptedJSONField
|
||||||
from kubernetes import client, config
|
from kubernetes import client, config
|
||||||
|
@ -19,6 +20,10 @@ from servala.core import rules as perms
|
||||||
from servala.core.models.mixins import ServalaModelMixin
|
from servala.core.models.mixins import ServalaModelMixin
|
||||||
from servala.core.validators import kubernetes_name_validator
|
from servala.core.validators import kubernetes_name_validator
|
||||||
|
|
||||||
|
SUPPORT_MESSAGE_TEMPLATE = _(
|
||||||
|
"Need help? We're happy to help via the <a href='{support_url}'>support form</a>."
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class ServiceCategory(ServalaModelMixin, models.Model):
|
class ServiceCategory(ServalaModelMixin, models.Model):
|
||||||
"""
|
"""
|
||||||
|
@ -615,10 +620,9 @@ class ServiceInstance(ServalaModelMixin, models.Model):
|
||||||
context=context,
|
context=context,
|
||||||
)
|
)
|
||||||
except IntegrityError:
|
except IntegrityError:
|
||||||
raise ValidationError(
|
raise cls._format_error_with_support(
|
||||||
_(
|
"An instance with this name already exists in this organization. Please choose a different name.",
|
||||||
"An instance with this name already exists in this organization. Please choose a different name."
|
organization,
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -657,10 +661,16 @@ class ServiceInstance(ServalaModelMixin, models.Model):
|
||||||
try:
|
try:
|
||||||
error_body = json.loads(e.body)
|
error_body = json.loads(e.body)
|
||||||
reason = error_body.get("message", str(e))
|
reason = error_body.get("message", str(e))
|
||||||
raise ValidationError(_("Kubernetes API error: {}").format(reason))
|
raise cls._format_error_with_support(
|
||||||
|
"Kubernetes API error: {error}.", organization, error=reason
|
||||||
|
)
|
||||||
except (ValueError, TypeError):
|
except (ValueError, TypeError):
|
||||||
raise ValidationError(_("Kubernetes API error: {}").format(str(e)))
|
raise cls._format_error_with_support(
|
||||||
raise ValidationError(_("Error creating instance: {}").format(str(e)))
|
"Kubernetes API error: {error}.", organization, error=str(e)
|
||||||
|
)
|
||||||
|
raise cls._format_error_with_support(
|
||||||
|
"Error creating instance: {error}.", organization, error=str(e)
|
||||||
|
)
|
||||||
return instance
|
return instance
|
||||||
|
|
||||||
def update_spec(self, spec_data, updated_by):
|
def update_spec(self, spec_data, updated_by):
|
||||||
|
@ -681,28 +691,27 @@ class ServiceInstance(ServalaModelMixin, models.Model):
|
||||||
self.save() # Updates updated_at timestamp
|
self.save() # Updates updated_at timestamp
|
||||||
except ApiException as e:
|
except ApiException as e:
|
||||||
if e.status == 404:
|
if e.status == 404:
|
||||||
raise ValidationError(
|
raise self._format_error_with_support(
|
||||||
_(
|
"Service instance not found in Kubernetes. It may have been deleted externally.",
|
||||||
"Service instance not found in Kubernetes. It may have been deleted externally."
|
self.organization,
|
||||||
)
|
|
||||||
)
|
)
|
||||||
try:
|
try:
|
||||||
error_body = json.loads(e.body)
|
error_body = json.loads(e.body)
|
||||||
reason = error_body.get("message", str(e))
|
reason = error_body.get("message", str(e))
|
||||||
raise ValidationError(
|
raise self._format_error_with_support(
|
||||||
_("Kubernetes API error updating instance: {error}").format(
|
"Kubernetes API error updating instance: {error}.",
|
||||||
error=reason
|
self.organization,
|
||||||
)
|
error=reason,
|
||||||
)
|
)
|
||||||
except (ValueError, TypeError):
|
except (ValueError, TypeError):
|
||||||
raise ValidationError(
|
raise self._format_error_with_support(
|
||||||
_("Kubernetes API error updating instance: {error}").format(
|
"Kubernetes API error updating instance: {error}.",
|
||||||
error=str(e)
|
self.organization,
|
||||||
)
|
error=str(e),
|
||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise ValidationError(
|
raise self._format_error_with_support(
|
||||||
_("Error updating instance: {error}").format(error=str(e))
|
"Error updating instance: {error}.", self.organization, error=str(e)
|
||||||
)
|
)
|
||||||
|
|
||||||
@transaction.atomic
|
@transaction.atomic
|
||||||
|
@ -854,3 +863,24 @@ class ServiceInstance(ServalaModelMixin, models.Model):
|
||||||
return {"error": str(e)}
|
return {"error": str(e)}
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return {"error": str(e)}
|
return {"error": str(e)}
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _format_error_with_support(cls, message, organization, **kwargs):
|
||||||
|
"""
|
||||||
|
Helper method to format error messages with support links.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
message: The error message template (without support text)
|
||||||
|
organization: The organization object to get the support URL
|
||||||
|
**kwargs: Additional format parameters for the message
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
A ValidationError with the formatted message including support link
|
||||||
|
"""
|
||||||
|
support_url = organization.urls.support
|
||||||
|
# Combine the main message with the support message template
|
||||||
|
full_message = _("{message} {support_message}").format(
|
||||||
|
message=_(message).format(**kwargs),
|
||||||
|
support_message=SUPPORT_MESSAGE_TEMPLATE.format(support_url=support_url),
|
||||||
|
)
|
||||||
|
return ValidationError(mark_safe(full_message))
|
||||||
|
|
|
@ -1,9 +1,14 @@
|
||||||
from django.utils.functional import cached_property
|
from django.utils.functional import cached_property
|
||||||
|
from django.utils.safestring import mark_safe
|
||||||
|
from django.utils.translation import gettext_lazy as _
|
||||||
from django.views.generic import UpdateView
|
from django.views.generic import UpdateView
|
||||||
from rules.contrib.views import AutoPermissionRequiredMixin, PermissionRequiredMixin
|
from rules.contrib.views import AutoPermissionRequiredMixin, PermissionRequiredMixin
|
||||||
|
|
||||||
from servala.core.models import Organization
|
from servala.core.models import Organization
|
||||||
|
|
||||||
|
# Import the support message template from the service model
|
||||||
|
from servala.core.models.service import SUPPORT_MESSAGE_TEMPLATE
|
||||||
|
|
||||||
|
|
||||||
class HtmxViewMixin:
|
class HtmxViewMixin:
|
||||||
fragments = []
|
fragments = []
|
||||||
|
@ -90,3 +95,22 @@ class OrganizationViewMixin(PermissionRequiredMixin):
|
||||||
|
|
||||||
def has_permission(self):
|
def has_permission(self):
|
||||||
return self.has_organization_permission() and super().has_permission()
|
return self.has_organization_permission() and super().has_permission()
|
||||||
|
|
||||||
|
def format_error_with_support(self, message, **kwargs):
|
||||||
|
"""
|
||||||
|
Helper method to format error messages with support links for frontend views.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
message: The error message template (without support text)
|
||||||
|
**kwargs: Additional format parameters for the message
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
A formatted message with support link
|
||||||
|
"""
|
||||||
|
support_url = self.organization.urls.support
|
||||||
|
# Combine the main message with the support message template
|
||||||
|
full_message = _("{message} {support_message}").format(
|
||||||
|
message=_(message).format(**kwargs),
|
||||||
|
support_message=SUPPORT_MESSAGE_TEMPLATE.format(support_url=support_url),
|
||||||
|
)
|
||||||
|
return mark_safe(full_message)
|
||||||
|
|
|
@ -144,7 +144,10 @@ class ServiceOfferingDetailView(OrganizationViewMixin, HtmxViewMixin, DetailView
|
||||||
|
|
||||||
form = self.get_instance_form()
|
form = self.get_instance_form()
|
||||||
if not form: # Should not happen if context_object is valid, but as a safeguard
|
if not form: # Should not happen if context_object is valid, but as a safeguard
|
||||||
messages.error(self.request, _("Could not initialize service form."))
|
messages.error(
|
||||||
|
self.request,
|
||||||
|
self.format_error_with_support("Could not initialize service form."),
|
||||||
|
)
|
||||||
return self.render_to_response(context)
|
return self.render_to_response(context)
|
||||||
|
|
||||||
if form.is_valid():
|
if form.is_valid():
|
||||||
|
@ -161,7 +164,10 @@ class ServiceOfferingDetailView(OrganizationViewMixin, HtmxViewMixin, DetailView
|
||||||
messages.error(self.request, e.message or str(e))
|
messages.error(self.request, e.message or str(e))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
messages.error(
|
messages.error(
|
||||||
self.request, _("Error creating instance: {}").format(str(e))
|
self.request,
|
||||||
|
self.format_error_with_support(
|
||||||
|
"Error creating instance: {error}.", error=str(e)
|
||||||
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
# If the form is not valid or if the service creation failed, we render it again
|
# If the form is not valid or if the service creation failed, we render it again
|
||||||
|
@ -366,7 +372,10 @@ class ServiceInstanceUpdateView(
|
||||||
return self.form_invalid(form)
|
return self.form_invalid(form)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
messages.error(
|
messages.error(
|
||||||
self.request, _("Error updating instance: {error}").format(error=str(e))
|
self.request,
|
||||||
|
self.format_error_with_support(
|
||||||
|
"Error updating instance: {error}.", error=str(e)
|
||||||
|
),
|
||||||
)
|
)
|
||||||
return self.form_invalid(form)
|
return self.form_invalid(form)
|
||||||
|
|
||||||
|
@ -435,9 +444,11 @@ class ServiceInstanceDeleteView(
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
messages.error(
|
messages.error(
|
||||||
self.request,
|
self.request,
|
||||||
_(
|
self.format_error_with_support(
|
||||||
"An error occurred while trying to delete instance '{name}': {error}"
|
"An error occurred while trying to delete instance '{name}': {error}.",
|
||||||
).format(name=self.object.name, error=str(e)),
|
name=self.object.name,
|
||||||
|
error=str(e),
|
||||||
|
),
|
||||||
)
|
)
|
||||||
response = HttpResponse()
|
response = HttpResponse()
|
||||||
response["HX-Redirect"] = str(self.object.urls.base)
|
response["HX-Redirect"] = str(self.object.urls.base)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue