Skip to content

Commit

Permalink
feat: give the error from the backend to the user, it contains more info
Browse files Browse the repository at this point in the history
  • Loading branch information
philipsens committed Aug 17, 2022
1 parent 66d9b6b commit 62bbcbf
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,13 @@ export class CurrentFileService {
this.currentFile.xml!
)
.then((response) => {
response.ok ? this.saveFileSuccessfully() : this.saveFileFailed();
if (response.ok) {
this.saveFileSuccessfully();
}
return response.json();
})
.then((body) => {
this.saveFileFailed(body);
});
}
}
Expand All @@ -238,11 +244,8 @@ export class CurrentFileService {
this.updateCurrentFile(this.currentFile);
}

saveFileFailed(): void {
this.toastr.error(
`The file ${this.currentFile.path} couldn't be saved.`,
'Error saving'
);
saveFileFailed(response: any): void {
this.toastr.error(response.error, 'Error saving');
}

updateCurrentFile(file: File): void {
Expand Down
27 changes: 15 additions & 12 deletions frank-flow/src/main/java/org/ibissource/frankflow/api/FileApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.ReadOnlyFileSystemException;
import java.nio.file.StandardCopyOption;


Expand Down Expand Up @@ -120,8 +121,10 @@ public Response createFolder(@PathParam("name") String configurationName, @Query
try (InputStream is = fileAttachment.getObject(InputStream.class)) {
Files.copy(is, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
return Response.status(Response.Status.OK).tag(eTag).build();
} catch (ReadOnlyFileSystemException e) {
throw new ApiException("The file '"+path+"' can't be saved on a read-only file system");
} catch (IOException e) {
throw new ApiException("an error occured while saving file ["+path+"]", e);
throw new ApiException("An error occurred while saving file ["+path+"]", e);
}
}

Expand All @@ -134,8 +137,8 @@ public Response createFolder(@PathParam("name") String configurationName, @Query
@Produces(MediaType.APPLICATION_JSON)
public Response renameFolder(@PathParam("name") String configurationName, @QueryParam("path") String path, @FormParam("newName") String newName) {

if(newName == null || newName == "") {
throw new ApiException("an unexpected error occured, property [newName] does not exist or is empty");
if(newName == null || newName.equals("")) {
throw new ApiException("An unexpected error occurred, property [newName] does not exist or is empty");
}


Expand All @@ -161,7 +164,7 @@ public Response renameFolder(@PathParam("name") String configurationName, @Query
if(file.renameTo(destFile)) {
return Response.status(Response.Status.OK).entity(path).type(MediaType.TEXT_PLAIN).build();
} else {
throw new ApiException("an unexpected error occured, file can't be renamed");
throw new ApiException("An unexpected error occurred, file can't be renamed");
}
}

Expand All @@ -180,20 +183,20 @@ public Response saveFile(@PathParam("name") String configurationName, @QueryPara
File rootFolder = FileUtils.getDir(configurationName);
File file = getFile(rootFolder, path);
if(file.exists()) {
throw new ApiException("file already exists", Response.Status.CONFLICT);
throw new ApiException("File already exists", Response.Status.CONFLICT);
}

try (InputStream is = fileAttachment.getObject(InputStream.class)) {
Files.copy(is, file.toPath(), StandardCopyOption.REPLACE_EXISTING);

// the file should always exists, lets make sure though, you never know...
// the file should always exist, lets make sure though, you never know...
if(file.exists()) {
EntityTag eTag = new EntityTag("lm"+file.lastModified());
return Response.status(Response.Status.OK).tag(eTag).build();
}
throw new ApiException("an unexpected error occured, file ["+path+"] does not exists");
throw new ApiException("An unexpected error occurred, file ["+path+"] does not exists");
} catch (IOException e) {
throw new ApiException("an error occured while saving file ["+path+"]", e);
throw new ApiException("An error occurred while saving file ["+path+"]", e);
}
}

Expand Down Expand Up @@ -224,7 +227,7 @@ public Response deleteFile(@PathParam("name") String configurationName, @QueryPa
if(file.delete()) {
return Response.status(Response.Status.OK).build();
} else {
throw new ApiException("unable to remove file ["+path+"]");
throw new ApiException("Unable to remove file ["+path+"]");
}
}

Expand All @@ -233,18 +236,18 @@ public Response deleteFile(@PathParam("name") String configurationName, @QueryPa
*/
private File getFile(File rootFolder, String path) {
if(path == null) {
throw new ApiException("no (valid) path specified");
throw new ApiException("No (valid) path specified");
}

File file = new File(rootFolder, path);
String normalizedFilename = FilenameUtils.normalize(file.getAbsolutePath());
if(normalizedFilename == null) { //non absolute path, perhaps ../ is used?
throw new ApiException("unable to determine normalized filename");
throw new ApiException("Unable to determine normalized filename");
}
else if(normalizedFilename.equals(file.getPath())) {
return file;
}

throw new ApiException("inaccessible path ["+file+"]");
throw new ApiException("Inaccessible path ["+file+"]");
}
}

0 comments on commit 62bbcbf

Please sign in to comment.