Skip to content

Commit 6de1d74

Browse files
committed
Move error handling from startProxy to runWrapper in start-proxy action
1 parent a506145 commit 6de1d74

File tree

2 files changed

+150
-145
lines changed

2 files changed

+150
-145
lines changed

lib/start-proxy-action.js

Lines changed: 71 additions & 67 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/start-proxy-action.ts

Lines changed: 79 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -89,40 +89,45 @@ async function runWrapper() {
8989

9090
const logger = getActionsLogger();
9191

92-
// Setup logging for the proxy
93-
const tempDir = actionsUtil.getTemporaryDirectory();
94-
const proxyLogFilePath = path.resolve(tempDir, "proxy.log");
95-
core.saveState("proxy-log-file", proxyLogFilePath);
96-
97-
// Get the configuration options
98-
const credentials = getCredentials(
99-
logger,
100-
actionsUtil.getOptionalInput("registry_secrets"),
101-
actionsUtil.getOptionalInput("registries_credentials"),
102-
actionsUtil.getOptionalInput("language"),
103-
);
92+
try {
93+
// Setup logging for the proxy
94+
const tempDir = actionsUtil.getTemporaryDirectory();
95+
const proxyLogFilePath = path.resolve(tempDir, "proxy.log");
96+
core.saveState("proxy-log-file", proxyLogFilePath);
10497

105-
if (credentials.length === 0) {
106-
logger.info("No credentials found, skipping proxy setup.");
107-
return;
108-
}
98+
// Get the configuration options
99+
const credentials = getCredentials(
100+
logger,
101+
actionsUtil.getOptionalInput("registry_secrets"),
102+
actionsUtil.getOptionalInput("registries_credentials"),
103+
actionsUtil.getOptionalInput("language"),
104+
);
109105

110-
logger.info(
111-
`Credentials loaded for the following registries:\n ${credentials
112-
.map((c) => credentialToStr(c))
113-
.join("\n")}`,
114-
);
106+
if (credentials.length === 0) {
107+
logger.info("No credentials found, skipping proxy setup.");
108+
return;
109+
}
110+
111+
logger.info(
112+
`Credentials loaded for the following registries:\n ${credentials
113+
.map((c) => credentialToStr(c))
114+
.join("\n")}`,
115+
);
115116

116-
const ca = generateCertificateAuthority();
117+
const ca = generateCertificateAuthority();
117118

118-
const proxyConfig: ProxyConfig = {
119-
all_credentials: credentials,
120-
ca,
121-
};
119+
const proxyConfig: ProxyConfig = {
120+
all_credentials: credentials,
121+
ca,
122+
};
122123

123-
// Start the Proxy
124-
const proxyBin = await getProxyBinaryPath(logger);
125-
await startProxy(proxyBin, proxyConfig, proxyLogFilePath, logger);
124+
// Start the Proxy
125+
const proxyBin = await getProxyBinaryPath(logger);
126+
await startProxy(proxyBin, proxyConfig, proxyLogFilePath, logger);
127+
} catch (unwrappedError) {
128+
const error = util.wrapError(unwrappedError);
129+
core.setFailed(`start-proxy action failed: ${error.message}`);
130+
}
126131
}
127132

128133
async function startProxy(
@@ -133,57 +138,53 @@ async function startProxy(
133138
) {
134139
const host = "127.0.0.1";
135140
let port = 49152;
136-
try {
137-
let subprocess: ChildProcess | undefined = undefined;
138-
let tries = 5;
139-
let subprocessError: Error | undefined = undefined;
140-
while (tries-- > 0 && !subprocess && !subprocessError) {
141-
subprocess = spawn(
142-
binPath,
143-
["-addr", `${host}:${port}`, "-config", "-", "-logfile", logFilePath],
144-
{
145-
detached: true,
146-
stdio: ["pipe", "ignore", "ignore"],
147-
},
148-
);
149-
subprocess.unref();
150-
if (subprocess.pid) {
151-
core.saveState("proxy-process-pid", `${subprocess.pid}`);
152-
}
153-
subprocess.on("error", (error) => {
154-
subprocessError = error;
155-
});
156-
subprocess.on("exit", (code) => {
157-
if (code !== 0) {
158-
// If the proxy failed to start, try a different port from the ephemeral range [49152, 65535]
159-
port = Math.floor(Math.random() * (65535 - 49152) + 49152);
160-
subprocess = undefined;
161-
}
162-
});
163-
subprocess.stdin?.write(JSON.stringify(config));
164-
subprocess.stdin?.end();
165-
// Wait a little to allow the proxy to start
166-
await util.delay(1000);
167-
}
168-
if (subprocessError) {
169-
// eslint-disable-next-line @typescript-eslint/only-throw-error
170-
throw subprocessError;
141+
let subprocess: ChildProcess | undefined = undefined;
142+
let tries = 5;
143+
let subprocessError: Error | undefined = undefined;
144+
while (tries-- > 0 && !subprocess && !subprocessError) {
145+
subprocess = spawn(
146+
binPath,
147+
["-addr", `${host}:${port}`, "-config", "-", "-logfile", logFilePath],
148+
{
149+
detached: true,
150+
stdio: ["pipe", "ignore", "ignore"],
151+
},
152+
);
153+
subprocess.unref();
154+
if (subprocess.pid) {
155+
core.saveState("proxy-process-pid", `${subprocess.pid}`);
171156
}
172-
logger.info(`Proxy started on ${host}:${port}`);
173-
core.setOutput("proxy_host", host);
174-
core.setOutput("proxy_port", port.toString());
175-
core.setOutput("proxy_ca_certificate", config.ca.cert);
176-
177-
const registry_urls = config.all_credentials
178-
.filter((credential) => credential.url !== undefined)
179-
.map((credential) => ({
180-
type: credential.type,
181-
url: credential.url,
182-
}));
183-
core.setOutput("proxy_urls", JSON.stringify(registry_urls));
184-
} catch (error) {
185-
core.setFailed(`start-proxy action failed: ${util.getErrorMessage(error)}`);
157+
subprocess.on("error", (error) => {
158+
subprocessError = error;
159+
});
160+
subprocess.on("exit", (code) => {
161+
if (code !== 0) {
162+
// If the proxy failed to start, try a different port from the ephemeral range [49152, 65535]
163+
port = Math.floor(Math.random() * (65535 - 49152) + 49152);
164+
subprocess = undefined;
165+
}
166+
});
167+
subprocess.stdin?.write(JSON.stringify(config));
168+
subprocess.stdin?.end();
169+
// Wait a little to allow the proxy to start
170+
await util.delay(1000);
171+
}
172+
if (subprocessError) {
173+
// eslint-disable-next-line @typescript-eslint/only-throw-error
174+
throw subprocessError;
186175
}
176+
logger.info(`Proxy started on ${host}:${port}`);
177+
core.setOutput("proxy_host", host);
178+
core.setOutput("proxy_port", port.toString());
179+
core.setOutput("proxy_ca_certificate", config.ca.cert);
180+
181+
const registry_urls = config.all_credentials
182+
.filter((credential) => credential.url !== undefined)
183+
.map((credential) => ({
184+
type: credential.type,
185+
url: credential.url,
186+
}));
187+
core.setOutput("proxy_urls", JSON.stringify(registry_urls));
187188
}
188189

189190
async function getProxyBinaryPath(logger: Logger): Promise<string> {

0 commit comments

Comments
 (0)