Skip to content

Commit bbd72d6

Browse files
committed
Fix error messages, #134
1 parent 3101eeb commit bbd72d6

File tree

4 files changed

+75
-2
lines changed

4 files changed

+75
-2
lines changed

jansi/src/main/java/org/fusesource/jansi/WindowsAnsiOutputStream.java

-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import java.io.IOException;
4040
import java.io.OutputStream; // expected diff with WindowsAnsiPrintStream.java
4141

42-
import org.fusesource.jansi.internal.WindowsSupport;
4342
import org.fusesource.jansi.internal.Kernel32.CONSOLE_SCREEN_BUFFER_INFO;
4443
import org.fusesource.jansi.internal.Kernel32.COORD;
4544

jansi/src/main/java/org/fusesource/jansi/WindowsAnsiPrintStream.java

-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import java.io.IOException;
4040
import java.io.PrintStream; // expected diff with WindowsAnsiOutputStream.java
4141

42-
import org.fusesource.jansi.internal.WindowsSupport;
4342
import org.fusesource.jansi.internal.Kernel32.CONSOLE_SCREEN_BUFFER_INFO;
4443
import org.fusesource.jansi.internal.Kernel32.COORD;
4544

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright (C) 2009-2019 the original author(s).
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.fusesource.jansi;
17+
18+
import java.io.UnsupportedEncodingException;
19+
20+
import static org.fusesource.jansi.internal.Kernel32.FORMAT_MESSAGE_FROM_SYSTEM;
21+
import static org.fusesource.jansi.internal.Kernel32.FormatMessageW;
22+
import static org.fusesource.jansi.internal.Kernel32.GetLastError;
23+
24+
public class WindowsSupport {
25+
26+
public static String getLastErrorMessage() {
27+
int errorCode = GetLastError();
28+
return getErrorMessage(errorCode);
29+
}
30+
31+
public static String getErrorMessage(int errorCode) {
32+
int bufferSize = 160;
33+
byte data[] = new byte[bufferSize];
34+
FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, 0, errorCode, 0, data, bufferSize, null);
35+
try {
36+
return new String(data, "UTF-16LE").trim();
37+
} catch (UnsupportedEncodingException e) {
38+
throw new IllegalStateException(e);
39+
}
40+
}
41+
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright (C) 2009-2019 the original author(s).
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.fusesource.jansi;
17+
18+
import org.junit.Test;
19+
20+
import static org.junit.Assert.assertEquals;
21+
import static org.junit.Assert.assertFalse;
22+
import static org.junit.Assert.assertNotNull;
23+
import static org.junit.Assume.assumeTrue;
24+
25+
public class WindowsSupportTest {
26+
27+
@Test
28+
public void testErrorMessage() {
29+
assumeTrue(AnsiConsole.IS_WINDOWS);
30+
String msg = WindowsSupport.getErrorMessage(500);
31+
assertEquals(msg, "User profile cannot be loaded.");
32+
}
33+
}

0 commit comments

Comments
 (0)