Skip to content

Commit

Permalink
#19646 Log error nicely (#20419)
Browse files Browse the repository at this point in the history
* #19646 Log error nicely

* remove import

* Rename AddFileNotPossibleException to FileCreationException
  • Loading branch information
freddyDOTCMS authored May 20, 2021
1 parent 991255a commit 7853e4b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.dotcms.publishing.PublisherConfig;
import com.dotmarketing.util.Config;

import com.dotmarketing.util.Logger;
import com.liferay.util.FileUtil;

import java.io.*;
Expand All @@ -29,7 +28,7 @@ public BundleOutput(final PublisherConfig publisherConfig){
* @return
* @throws IOException
*/
public abstract OutputStream addFile(String filePath) throws IOException;
public abstract OutputStream addFile(String filePath) throws FileCreationException;

/**
* Add a new file into the output
Expand All @@ -38,7 +37,7 @@ public BundleOutput(final PublisherConfig publisherConfig){
* @return
* @throws IOException
*/
public OutputStream addFile(File file) throws IOException {
public OutputStream addFile(File file) throws FileCreationException {
return addFile(file.getPath());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@

import com.dotcms.publishing.BundlerUtil;
import com.dotcms.publishing.PublisherConfig;
import com.dotmarketing.beans.Host;
import com.dotmarketing.util.WebKeys;
import com.google.common.annotations.VisibleForTesting;
import com.liferay.util.FileUtil;
import io.vavr.Lazy;
import io.vavr.control.Try;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.FileFilter;
Expand Down Expand Up @@ -58,15 +53,19 @@ public void setLastModified(final String filePath, final long timeInMillis) {
}

@Override
public OutputStream addFile(final String filePath) throws IOException {
public OutputStream addFile(final String filePath) throws FileCreationException {
final File fileAbsolute = getRealFile(filePath);
fileAbsolute.getParentFile().mkdirs();

if (!fileAbsolute.exists()) {
fileAbsolute.createNewFile();
}
try {
if (!fileAbsolute.exists()) {
fileAbsolute.createNewFile();
}

return Files.newOutputStream( fileAbsolute.toPath());
return Files.newOutputStream(fileAbsolute.toPath());
} catch (IOException e) {
throw new FileCreationException(e, filePath);
}
}

private File getRealFile(final String path) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.dotcms.publishing.output;


import java.io.IOException;

public class FileCreationException extends IOException {

private String filePath;

public FileCreationException(final Throwable cause, final String filePath) {
super(cause);
this.filePath = filePath;
}

@Override
public String getMessage(){
final Throwable cause = this.getCause();

final String message = cause.getMessage().contains("Not a directory") ?
"At least one subfolder already exists as a file" : cause.getMessage();

return String.format("It is not possible create the File: %s because: %s", filePath,
message);
}
}

0 comments on commit 7853e4b

Please sign in to comment.