Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: #6087 Display specific error message for course slug collision (revision) #6168

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions app/assets/javascripts/utils/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,10 +326,14 @@ const API = {
this.status = response.statusText;
SentryLogger.obj = this.obj;
SentryLogger.status = this.status;
Sentry.captureMessage('saveCourse failed', {
level: 'error',
extra: SentryLogger
});
try {
Sentry.captureMessage('saveCourse failed', {
level: 'error',
extra: SentryLogger
});
} catch (error) {
console.error('Sentry.captureMessage failed:', error);
}
response.responseText = data;
throw response;
}
Expand Down
11 changes: 8 additions & 3 deletions app/controllers/courses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,15 @@ def update
ensure_passcode_set
UpdateCourseWorker.schedule_edits(course: @course, editing_user: current_user)
render json: { course: @course }
rescue Course::DuplicateCourseSlugError => e
render_error(:unprocessable_entity, 'courses.error.duplicate_slug', slug: e.slug)
rescue Wiki::InvalidWikiError => e
message = I18n.t('courses.error.invalid_wiki', domain: e.domain)
render json: { errors: e, message: },
status: :not_found
render_error(:not_found, 'courses.error.invalid_wiki', domain: e.domain)
end

def render_error(status, error_key, **params)
render json: { errors: params, message: I18n.t(error_key, **params) },
status:
end

def destroy
Expand Down
18 changes: 18 additions & 0 deletions app/models/course.rb
Original file line number Diff line number Diff line change
Expand Up @@ -593,4 +593,22 @@ def check_course_times
def set_needs_update
self.needs_update = true if start_changed?
end

# Handle duplicate slug collision
validate :check_duplicate_slug, on: :update

def check_duplicate_slug
return unless Course.where(slug:).where.not(id:).exists?

raise DuplicateCourseSlugError, slug
end

class DuplicateCourseSlugError < StandardError
attr_reader :slug

def initialize(slug, msg = 'Duplicate Slug')
@slug = slug
super(msg)
end
end
end
Loading