Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(publisher-electron-release-server): throw an exception for 4xx/5xx HTTP status codes #1538

Merged
merged 1 commit into from
Mar 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/publisher/electron-release-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
"typings": "dist/PublisherERS.d.ts",
"devDependencies": {
"chai": "4.2.0",
"mocha": "^7.1.0"
"mocha": "^7.1.0",
"proxyquire": "^2.1.3",
"fetch-mock": "^9.0.0"
},
"engines": {
"node": ">= 10.0.0"
Expand Down
17 changes: 14 additions & 3 deletions packages/publisher/electron-release-server/src/PublisherERS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { asyncOra } from '@electron-forge/async-ora';
import { ForgePlatform, ForgeArch } from '@electron-forge/shared-types';

import debug from 'debug';
import fetch from 'node-fetch';
import fetch, { RequestInfo, RequestInit, Response } from 'node-fetch';
import FormData from 'form-data';
import fs from 'fs-extra';
import path from 'path';
Expand All @@ -17,6 +17,17 @@ interface ERSVersion {
assets: { name: string; }[];
}

const fetchAndCheckStatus = async (
url: RequestInfo,
init?: RequestInit,
): Promise<Response> => {
const result = await fetch(url, init);
if (result.ok) { // res.status >= 200 && res.status < 300
return result;
}
throw new Error(`ERS publish failed with status code: ${result.status} (${result.url})`);
};

export const ersPlatform = (platform: ForgePlatform, arch: ForgeArch) => {
switch (platform) {
case 'darwin':
Expand Down Expand Up @@ -44,7 +55,7 @@ export default class PublisherERS extends PublisherBase<PublisherERSConfig> {

const api = (apiPath: string) => `${config.baseUrl}/${apiPath}`;

const { token } = await (await fetch(api('api/auth/login'), {
const { token } = await (await fetchAndCheckStatus(api('api/auth/login'), {
method: 'POST',
body: JSON.stringify({
username: config.username,
Expand All @@ -56,7 +67,7 @@ export default class PublisherERS extends PublisherBase<PublisherERSConfig> {
})).json();

// eslint-disable-next-line max-len
const authFetch = (apiPath: string, options?: any) => fetch(api(apiPath), { ...options || {}, headers: { ...(options || {}).headers, Authorization: `Bearer ${token}` } });
const authFetch = (apiPath: string, options?: any) => fetchAndCheckStatus(api(apiPath), { ...options || {}, headers: { ...(options || {}).headers, Authorization: `Bearer ${token}` } });

const versions: ERSVersion[] = await (await authFetch('api/version')).json();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { expect } from 'chai';
import { ForgeConfig } from '@electron-forge/shared-types';
import fetchMock from 'fetch-mock';
import proxyquire from 'proxyquire';

describe('PublisherERS', () => {
let fetch: typeof fetchMock;
beforeEach(() => {
fetch = fetchMock.sandbox();
});
it('fail if the server returns 4xx', async () => {
fetch.mock('begin:http://example.com', { body: {}, status: 400 });
const PublisherERS = proxyquire.noCallThru().load('../src/PublisherERS', {
'node-fetch': fetch,
}).default;


const publisher = new PublisherERS({
baseUrl: 'http://example.com',
username: 'test',
password: 'test',
});
return expect(publisher.publish({ makeResults: [], dir: '', forgeConfig: {} as ForgeConfig })).to.eventually.be.rejectedWith('ERS publish failed with status code: 400 (http://example.com/api/auth/login)');
});

afterEach(() => {
fetch.restore();
});
});