Skip to content

Commit

Permalink
fix(publisher-electron-release-server): throw an exception for 4xx/5x…
Browse files Browse the repository at this point in the history
…x HTTP status codes (#1538)
  • Loading branch information
pierrickouw authored Mar 3, 2020
1 parent 28dbb19 commit f223fc8
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
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();
});
});

0 comments on commit f223fc8

Please sign in to comment.