Skip to content

Commit

Permalink
fix(clients): better missing region error thrown
Browse files Browse the repository at this point in the history
  • Loading branch information
shortcuts committed Aug 10, 2022
1 parent 082a59a commit 37d4235
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 36 deletions.
21 changes: 4 additions & 17 deletions templates/java/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -99,20 +99,7 @@ public class {{classname}} extends ApiClient {
private static List<StatefulHost> getDefaultHosts(String region) throws AlgoliaRuntimeException {
List<StatefulHost> hosts = new ArrayList<StatefulHost>();
{{^fallbackToAliasHost}}
boolean found = false;
if (region == null) {
throw new AlgoliaRuntimeException("`region` is missing");
}
for (String allowed : allowedRegions) {
if (allowed.equals(region)) {
found = true;
break;
}
}
{{/fallbackToAliasHost}}
{{#fallbackToAliasHost}}
boolean found = region == null;
boolean found = {{^fallbackToAliasHost}}false{{/fallbackToAliasHost}}{{#fallbackToAliasHost}}region == null{{/fallbackToAliasHost}};
if (region != null) {
for (String allowed : allowedRegions) {
if (allowed.equals(region)) {
Expand All @@ -121,9 +108,9 @@ public class {{classname}} extends ApiClient {
}
}
}
{{/fallbackToAliasHost}}
if (!found) {
throw new AlgoliaRuntimeException("`region` must be one of the following: {{#allowedRegions}}{{.}}{{^-last}}, {{/-last}}{{/allowedRegions}}");

if ({{^fallbackToAliasHost}}region == null || {{/fallbackToAliasHost}}!found){
throw new AlgoliaRuntimeException("`region` {{^fallbackToAliasHost}}is required and {{/fallbackToAliasHost}}must be one of the following: {{#allowedRegions}}{{.}}{{^-last}}, {{/-last}}{{/allowedRegions}}");
}

String url = {{#fallbackToAliasHost}}region == null ? "{{{hostWithFallback}}}" : {{/fallbackToAliasHost}} "{{{host}}}".replace("{region}", region);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,13 @@ function initAbtesting(initOptions: InitClientOptions & InitClientRegion<Abtesti
}

function initPersonalization(initOptions: InitClientOptions & Required<InitClientRegion<PersonalizationRegion>>): PersonalizationClient {
if (!initOptions.region) {
throw new Error('`region` is missing.');
}

if (
initOptions.region &&
!initOptions.region || (initOptions.region &&
(typeof initOptions.region !== 'string' ||
!personalizationRegions.includes(initOptions.region))
!personalizationRegions.includes(initOptions.region)))
) {
throw new Error(
`\`region\` must be one of the following: ${personalizationRegions.join(', ')}`
`\`region\` is required and must be one of the following: ${personalizationRegions.join(', ')}`
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,8 @@ if (!apiKey || typeof apiKey !== 'string') {
}

{{#hasRegionalHost}}
{{^fallbackToAliasHost}}
if (!region) {
throw new Error("`region` is missing.");
}
{{/fallbackToAliasHost}}

if (region && (typeof region !== 'string' || !REGIONS.includes(region))) {
throw new Error(`\`region\` must be one of the following: ${REGIONS.join(', ')}`);
if ({{^fallbackToAliasHost}}!region || {{/fallbackToAliasHost}}(region && (typeof region !== 'string' || !REGIONS.includes(region)))) {
throw new Error(`\`region\` {{^fallbackToAliasHost}}is required and {{/fallbackToAliasHost}}must be one of the following: ${REGIONS.join(', ')}`);
}
{{/hasRegionalHost}}
19 changes: 18 additions & 1 deletion tests/CTS/client/personalization/parameters.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
[
{
"testName": "throws when region is not given",
"autoCreateClient": false,
"steps": [
{
"type": "createClient",
"parameters": {
"appId": "my-app-id",
"apiKey": "my-api-key",
"region": ""
},
"expected": {
"error": "`region` is required and must be one of the following: eu, us"
}
}
]
},
{
"testName": "throws when incorrect region is given",
"autoCreateClient": false,
Expand All @@ -11,7 +28,7 @@
"region": "not_a_region"
},
"expected": {
"error": "`region` must be one of the following: eu, us"
"error": "`region` is required and must be one of the following: eu, us"
}
}
]
Expand Down
19 changes: 18 additions & 1 deletion tests/CTS/client/predict/parameters.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
[
{
"testName": "throws when region is not given",
"autoCreateClient": false,
"steps": [
{
"type": "createClient",
"parameters": {
"appId": "my-app-id",
"apiKey": "my-api-key",
"region": ""
},
"expected": {
"error": "`region` is required and must be one of the following: ue, ew"
}
}
]
},
{
"testName": "throws when incorrect region is given",
"autoCreateClient": false,
Expand All @@ -11,7 +28,7 @@
"region": "not_a_region"
},
"expected": {
"error": "`region` must be one of the following: ue, ew"
"error": "`region` is required and must be one of the following: ue, ew"
}
}
]
Expand Down
19 changes: 18 additions & 1 deletion tests/CTS/client/query-suggestions/parameters.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
[
{
"testName": "throws when region is not given",
"autoCreateClient": false,
"steps": [
{
"type": "createClient",
"parameters": {
"appId": "my-app-id",
"apiKey": "my-api-key",
"region": ""
},
"expected": {
"error": "`region` is required and must be one of the following: eu, us"
}
}
]
},
{
"testName": "throws when incorrect region is given",
"autoCreateClient": false,
Expand All @@ -11,7 +28,7 @@
"region": "not_a_region"
},
"expected": {
"error": "`region` must be one of the following: eu, us"
"error": "`region` is required and must be one of the following: eu, us"
}
}
]
Expand Down
4 changes: 2 additions & 2 deletions tests/CTS/client/sources/parameters.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"region": ""
},
"expected": {
"error": "`region` is missing."
"error": "`region` is required and must be one of the following: de, us"
}
}
]
Expand All @@ -28,7 +28,7 @@
"region": "not_a_region"
},
"expected": {
"error": "`region` must be one of the following: de, us"
"error": "`region` is required and must be one of the following: de, us"
}
}
]
Expand Down

0 comments on commit 37d4235

Please sign in to comment.