Skip to content

Commit

Permalink
#11736 do not rethrow already thrown exception
Browse files Browse the repository at this point in the history
Signed-off-by: Ludovic Orban <lorban@bitronix.be>
  • Loading branch information
lorban committed May 13, 2024
1 parent 980576d commit c54141a
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class ContentSinkOutputStream extends OutputStream
{
private final Blocker.Shared _blocking = new Blocker.Shared();
private final Content.Sink sink;
private boolean failed;

public ContentSinkOutputStream(Content.Sink sink)
{
Expand All @@ -55,7 +56,7 @@ public void write(byte[] b, int off, int len) throws IOException
}
catch (Throwable x)
{
throw IO.rethrow(x);
handleException(x);
}
}

Expand All @@ -69,7 +70,7 @@ public void flush() throws IOException
}
catch (Throwable x)
{
throw IO.rethrow(x);
handleException(x);
}
}

Expand All @@ -83,12 +84,20 @@ public void close() throws IOException
}
catch (Throwable x)
{
throw IO.rethrow(x);
handleException(x);
}
}

public void close(Callback callback) throws IOException
{
sink.write(true, null, callback);
}

private void handleException(Throwable x) throws IOException
{
if (failed)
throw new IOException(x.toString());
failed = true;
throw IO.rethrow(x);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

package org.eclipse.jetty.io.content;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;

import org.eclipse.jetty.io.Content;
import org.eclipse.jetty.io.EofException;
import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not;
import static org.junit.jupiter.api.Assertions.fail;

public class ContentSinkOutputStreamTest
{
@Test
public void testNoDuplicateExceptionThrown()
{
EofException eofException = new EofException();
Content.Sink sink = (last, byteBuffer, callback) -> callback.failed(eofException);

StringWriter stringWriter = new StringWriter();
try (OutputStream body = new ContentSinkOutputStream(sink))
{
try
{
body.write('a');
fail("expected IOException in write()");
}
catch (IOException e)
{
body.flush();
fail("expected IOException in flush()");
}
}
catch (IOException ex)
{
ex.printStackTrace(new PrintWriter(stringWriter));
}
assertThat(stringWriter.toString(), not(containsString("CIRCULAR REFERENCE")));
}
}

0 comments on commit c54141a

Please sign in to comment.