make error handling on unreachable server to be explicit (#4852)

This commit is contained in:
Saturnino Abril 2020-10-08 22:19:29 +08:00 committed by GitHub
parent 361349a4d4
commit 335a25b516
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 40 additions and 16 deletions

View file

@ -5,6 +5,7 @@ import {System, User} from '@support/server_api';
beforeAll(async () => {
// Login as sysadmin and reset server configuration
await System.apiCheckSystemHealth();
await User.apiAdminLogin();
await System.apiUpdateConfig();

View file

@ -2,7 +2,20 @@
// See LICENSE.txt for license information.
export const getResponseFromError = (err) => {
const {response: {data, status}} = err;
const {response} = err;
if (!response) {
const message = `No response from server at "${err.config.baseURL}".
If testing against a server other than the default local instance, you may set the server URL via "SITE_URL" environment variable.
`;
// Throw an error instead of failing silently
throw new Error(message);
}
const {data, status} = response;
// Explicitly print out response data from server for ease of debugging
console.warn(data); // eslint-disable-line no-console
return {error: data, status};
};

View file

@ -4,7 +4,6 @@
import Channel from './channel';
import Team from './team';
import User from './user';
import {getResponseFromError} from './common';
/**
* Creates new user, channel and team for test isolation.
@ -16,22 +15,18 @@ export const apiInit = async ({
teamOptions = {type: 'O', prefix: 'team'},
userOptions = {prefix: 'user'},
} = {}) => {
try {
const {team} = await Team.apiCreateTeam(teamOptions);
const {channel} = await Channel.apiCreateChannel({...channelOptions, teamId: team.id});
const {user} = await User.apiCreateUser(userOptions);
const {team} = await Team.apiCreateTeam(teamOptions);
const {channel} = await Channel.apiCreateChannel({...channelOptions, teamId: team.id});
const {user} = await User.apiCreateUser(userOptions);
await Team.apiAddUserToTeam(user.id, team.id);
await Channel.apiAddUserToChannel(user.id, channel.id);
await Team.apiAddUserToTeam(user.id, team.id);
await Channel.apiAddUserToChannel(user.id, channel.id);
return {
channel,
team,
user,
};
} catch (err) {
return getResponseFromError(err);
}
return {
channel,
team,
user,
};
};
export const Setup = {

View file

@ -21,6 +21,20 @@ import defaultServerConfig from './default_config.json';
// - return value defined by `@return`
// ****************************************************************
/**
* Check system health.
* See https://api.mattermost.com/#tag/system/paths/~1system~1ping/get
* @return {Object} returns {data} on success or {error, status} on error
*/
export const apiCheckSystemHealth = async () => {
try {
const response = await client.get('/api/v4/system/ping?get_server_status=true');
return {data: response.data};
} catch (err) {
return getResponseFromError(err);
}
};
/**
* Get configuration.
* See https://api.mattermost.com/#tag/system/paths/~1config/get
@ -65,6 +79,7 @@ const getDefaultConfig = () => {
};
export const System = {
apiCheckSystemHealth,
apiGetConfig,
apiUpdateConfig,
};