Skip to content

Commit 9870635

Browse files
author
Shuai Zhang
committed
WS-2092: Unify the response format in case of an error at the service level
1 parent 9bf9a40 commit 9870635

File tree

6 files changed

+41
-10
lines changed

6 files changed

+41
-10
lines changed

src/lua/api-gateway/validation/validatorsHandlerErrorDecorator.lua

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,10 @@ function ValidatorHandlerErrorDecorator:decorateResponse( response_status, respo
100100
ngx.header[k] = val
101101
end
102102
end
103-
ngx.say(o.message)
103+
-- ngx.say(o.message)
104+
-- add custom message
105+
local msg = self:parseResponseMessage(o.message)
106+
ngx.say(msg)
104107
return
105108
end
106109

@@ -114,6 +117,29 @@ function ValidatorHandlerErrorDecorator:decorateResponse( response_status, respo
114117
ngx.exit( response_status )
115118
end
116119

120+
--- Parse the response message and replace any variables, if found (at most 3 variables)
121+
-- @param message Response message
122+
--
123+
function ValidatorHandlerErrorDecorator:parseResponseMessage(message)
124+
local m = message
125+
local from, to, var, varName, value
126+
local cnt = 0
127+
while cnt < 3 do
128+
from, to = ngx.re.find(m, "ngx.var.[a-zA-Z_0-9]+", "jo")
129+
if(from) then
130+
var = string.sub(m, from, to)
131+
varName = string.sub(m, from + 8, to) -- "+ 8" jump over "ngx.var."
132+
value = ngx.var[varName]
133+
m = string.gsub(m, var, value)
134+
else
135+
break
136+
end
137+
cnt = cnt + 1
138+
end
139+
-- all variables have been replaced
140+
return m
141+
end
142+
117143
-- hook to overwrite the DEFAULT_RESPONSES by specifying a jsonString
118144
function ValidatorHandlerErrorDecorator:setUserDefinedResponsesFromJson( jsonString )
119145
if ( jsonString == nil or #jsonString < 2) then

test/perl/api-gateway/validation/key/api_key.t

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ GET /test-api-key?api_key=ab123
132132
--- config
133133
include ../../api-gateway/api_key_service.conf;
134134
include ../../api-gateway/default_validators.conf;
135+
error_log ../test-logs/api_key_test5_error.log debug;
135136
location /test-api-key-5 {
136137
set $service_id s-123;
137138

test/perl/api-gateway/validation/oauth2/oauthTokenValidator.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ GET /test-oauth-validation
109109
110110
set $key 'cachedoauth:$authtoken_hash';
111111
content_by_lua '
112-
local BaseValidator = require "api-gateway.core.validator"
112+
local BaseValidator = require "api-gateway.validation.validator"
113113
local TestValidator = BaseValidator:new()
114114
local validator = TestValidator:new()
115115
local res = validator:getKeyFromRedis(ngx.var.key, "token_json")

test/perl/api-gateway/validation/oauth2/userProfileValidator.t

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ __DATA__
7373
set_md5 $authtoken_hash $authtoken;
7474
set $key 'cachedoauth:$authtoken_hash';
7575
content_by_lua '
76-
local BaseValidator = require "api-gateway.core.validator"
76+
local BaseValidator = require "api-gateway.validation.validator"
7777
local v = BaseValidator:new()
7878
local k = v:getKeyFromLocalCache(ngx.var.key,"cachedUserProfiles")
7979
v:exitFn(200,"Local: " .. tostring(k))
@@ -87,7 +87,7 @@ __DATA__
8787
set_md5 $authtoken_hash $authtoken;
8888
set $key 'cachedoauth:$authtoken_hash';
8989
content_by_lua '
90-
local BaseValidator = require "api-gateway.core.validator"
90+
local BaseValidator = require "api-gateway.validation.validator"
9191
local v = BaseValidator:new()
9292
local k = v:getKeyFromRedis(ngx.var.key,"user_json")
9393
v:exitFn(200,"Redis: " .. tostring(k))
@@ -136,7 +136,7 @@ Authorization: Bearer SOME_OAUTH_PROFILE_TEST_1
136136
137137
location /local-cache {
138138
content_by_lua '
139-
local BaseValidator = require "api-gateway.core.validator"
139+
local BaseValidator = require "api-gateway.validation.validator"
140140
local v = BaseValidator:new()
141141
local k = v:getKeyFromLocalCache("cachedoauth:8cd12eadb5032aa2153c8f830d01e0be","cachedUserProfiles")
142142
v:exitFn(200,k)
@@ -145,7 +145,7 @@ Authorization: Bearer SOME_OAUTH_PROFILE_TEST_1
145145
146146
location /redis-cache {
147147
content_by_lua '
148-
local BaseValidator = require "api-gateway.core.validator"
148+
local BaseValidator = require "api-gateway.validation.validator"
149149
local v = BaseValidator:new()
150150
local k = v:getKeyFromRedis("cachedoauth:8cd12eadb5032aa2153c8f830d01e0be","user_json")
151151
v:exitFn(200,k)

test/perl/api-gateway/validation/validatorHandler.t

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ Content-Type: text/plain"
330330
"VALIDATOR_401_ERROR" : {
331331
"http_status" : 503,
332332
"error_code" : 401,
333-
"message" : "{\\"error_code\\": \\"-1\\",\\"message\\":\\"custom error message\\"}",
333+
"message" : "{\\"error_code\\": \\"-1\\",\\"message\\":\\"custom error message\\", \\"requestID\\":\\"ngx.var.requestID\\", \\"another_ID\\":\\"ngx.var.another_ID\\", \\"var3\\":\\"ngx.var.var3\\", \\"var4\\":\\"ngx.var.var4\\"}",
334334
"headers" : {
335335
"custom-header-1": "header-1-value",
336336
"custom-header-2": "ngx.var.custom_header_2"
@@ -354,6 +354,10 @@ Content-Type: text/plain"
354354
set $request_validator_1 "on; path=/validator_1; order=1;";
355355
set $request_validator_2 "on; path=/validator_2; order=2;";
356356
set $custom_header_2 "this is a lua variable";
357+
set $requestID "message customization finished";
358+
set $another_ID "another field";
359+
set $var3 "var3 value";
360+
set $var4 "var4 value";
357361
358362
access_by_lua "ngx.apiGateway.validation.validateRequest()";
359363
content_by_lua '
@@ -385,7 +389,7 @@ Content-Type: text/plain"
385389
]
386390
--- response_body_like eval
387391
[
388-
'^{"error_code": "-1","message":"custom error message"}+',
392+
'^{"error_code": "-1","message":"custom error message", "requestID":"message customization finished", "another_ID":"another field", "var3":"var3 value", "var4":"ngx.var.var4"}+',
389393
'^{"error_code":"403010","message":"Oauth token is missing."}+',
390394
"You did not send any key"
391395
]

test/resources/api-gateway/api_key_service.conf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ location ~ /cache/api_key/get {
9292
deny all;
9393
}
9494

95-
set $key $arg_key;
95+
set $api_key $arg_key;
9696
set $service_id $arg_service_id;
9797

9898
# set $redis_cmd "HMGET cachedkey:$key:$service_id service-id service-name realm consumer-org-name app-name plan-name";
@@ -109,7 +109,7 @@ location ~ /cache/api_key/get {
109109
end;
110110
';
111111

112-
content_by_lua_file /opt/ADBE/api-gateway/api-gateway-core/scripts/api_key_validator.lua;
112+
content_by_lua 'ngx.apiGateway.validation.validateApiKey()';
113113
}
114114

115115
# pure REST API URI where POST goes to /set, GET to /get, DELETE to /del through internal redirect

0 commit comments

Comments
 (0)