Skip to content

Commit aa0c2e2

Browse files
committed
Handle NotePathAlreadyExistsException with ConflictException
- Introduced `ConflictException` for detailed error response. - Added handling of `NotePathAlreadyExistsException` in `NotebookRestApi`.
1 parent 8ef350e commit aa0c2e2

File tree

4 files changed

+68
-3
lines changed

4 files changed

+68
-3
lines changed

zeppelin-server/src/main/java/org/apache/zeppelin/rest/NotebookRestApi.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,11 @@
4646
import org.apache.zeppelin.notebook.Notebook;
4747
import org.apache.zeppelin.notebook.Paragraph;
4848
import org.apache.zeppelin.notebook.AuthorizationService;
49+
import org.apache.zeppelin.notebook.exception.NotePathAlreadyExistsException;
4950
import org.apache.zeppelin.notebook.repo.NotebookRepoWithVersionControl;
5051
import org.apache.zeppelin.notebook.scheduler.SchedulerService;
5152
import org.apache.zeppelin.rest.exception.BadRequestException;
53+
import org.apache.zeppelin.rest.exception.ConflictException;
5254
import org.apache.zeppelin.rest.exception.ForbiddenException;
5355
import org.apache.zeppelin.rest.exception.NoteNotFoundException;
5456
import org.apache.zeppelin.rest.exception.ParagraphNotFoundException;
@@ -486,7 +488,15 @@ public Response exportNote(@PathParam("noteId") String noteId) throws IOExceptio
486488
@ZeppelinApi
487489
public Response importNote(@QueryParam("notePath") String notePath, String noteJson) throws IOException {
488490
String noteId = notebookService.importNote(notePath, noteJson, getServiceContext(),
489-
new RestServiceCallback<>());
491+
new RestServiceCallback<>() {
492+
@Override
493+
public void onFailure(Exception ex, ServiceContext context) throws IOException {
494+
if (ex instanceof NotePathAlreadyExistsException) {
495+
ex = new ConflictException(ex.getMessage());
496+
}
497+
super.onFailure(ex, context);
498+
}
499+
});
490500
return new JsonResponse<>(Status.OK, "", noteId).build();
491501
}
492502

@@ -512,7 +522,15 @@ public Response createNote(String message) throws IOException {
512522
defaultInterpreterGroup,
513523
request.getAddingEmptyParagraph(),
514524
getServiceContext(),
515-
new RestServiceCallback<>());
525+
new RestServiceCallback<>() {
526+
@Override
527+
public void onFailure(Exception ex, ServiceContext context) throws IOException {
528+
if (ex instanceof NotePathAlreadyExistsException) {
529+
ex = new ConflictException(ex.getMessage());
530+
}
531+
super.onFailure(ex, context);
532+
}
533+
});
516534
return notebook.processNote(noteId,
517535
note -> {
518536
AuthenticationInfo subject = new AuthenticationInfo(authenticationService.getPrincipal());
@@ -613,6 +631,13 @@ public void onSuccess(Note note, ServiceContext context) throws IOException {
613631
notebookServer.broadcastNote(note);
614632
notebookServer.broadcastNoteList(context.getAutheInfo(), context.getUserAndRoles());
615633
}
634+
@Override
635+
public void onFailure(Exception ex, ServiceContext context) throws IOException {
636+
if (ex instanceof NotePathAlreadyExistsException) {
637+
ex = new ConflictException(ex.getMessage());
638+
}
639+
super.onFailure(ex, context);
640+
}
616641
});
617642
return new JsonResponse<>(Status.OK, "").build();
618643
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.zeppelin.rest.exception;
18+
19+
import jakarta.ws.rs.WebApplicationException;
20+
import jakarta.ws.rs.core.Response;
21+
import org.apache.zeppelin.utils.ExceptionUtils;
22+
23+
/**
24+
* ConflictException handler for WebApplicationException
25+
*/
26+
public class ConflictException extends WebApplicationException {
27+
private static Response conflictJson(String message) {
28+
return ExceptionUtils.jsonResponseContent(Response.Status.CONFLICT, message);
29+
}
30+
31+
public ConflictException(String message) {
32+
super(conflictJson(message));
33+
}
34+
}

zeppelin-server/src/main/java/org/apache/zeppelin/service/NotebookService.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,9 @@ public String importNote(String notePath,
385385
callback.onSuccess(note, context);
386386
return note.getId();
387387
});
388-
388+
} catch (NotePathAlreadyExistsException e) {
389+
callback.onFailure(new NotePathAlreadyExistsException("Fail to import note: " + e.getMessage(), e), context);
390+
return null;
389391
} catch (IOException e) {
390392
callback.onFailure(new IOException("Fail to import note: " + e.getMessage(), e), context);
391393
return null;

zeppelin-zengine/src/main/java/org/apache/zeppelin/notebook/exception/NotePathAlreadyExistsException.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,8 @@ public NotePathAlreadyExistsException(final String message) {
2626
super(message);
2727
}
2828

29+
public NotePathAlreadyExistsException(final String message, final Throwable cause) {
30+
super(message, cause);
31+
}
32+
2933
}

0 commit comments

Comments
 (0)