Skip to content

Commit d8c3c63

Browse files
authored
fix: error handling clean up (#84)
1 parent 84e02b7 commit d8c3c63

File tree

4 files changed

+24
-20
lines changed

4 files changed

+24
-20
lines changed

src/responses/index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
* OF ANY KIND, either express or implied. See the License for the specific language
1010
* governing permissions and limitations under the License.
1111
*/
12+
import { DEFAULT_UNAUTHORIZED_HTML_MESSAGE } from '../utils/constants.js';
13+
1214
export function daResp({
1315
body, status, contentType, contentLength,
1416
}) {
@@ -27,8 +29,12 @@ export function daResp({
2729
return new Response(body, { status, headers });
2830
}
2931

30-
export function get404() {
31-
return daResp({ body: '', status: 404 });
32+
export function get404(message = '') {
33+
return daResp({ body: message, status: 404, contentType: 'text/html' });
34+
}
35+
36+
export function get401(message = DEFAULT_UNAUTHORIZED_HTML_MESSAGE) {
37+
return daResp({ body: message, status: 404, contentType: 'text/html' });
3238
}
3339

3440
export function getRobots() {

src/routes/cookie.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* OF ANY KIND, either express or implied. See the License for the specific language
1010
* governing permissions and limitations under the License.
1111
*/
12-
import { daResp } from '../responses/index.js';
12+
import { daResp, get401 } from '../responses/index.js';
1313
import { DEFAULT_CORS_HEADERS, TRUSTED_ORIGINS } from '../utils/constants.js';
1414

1515
export function getCookie({ req }) {
@@ -33,5 +33,5 @@ export function getCookie({ req }) {
3333
return new Response('cookie set', { headers: respHeaders });
3434
}
3535
}
36-
return daResp({ body: '401 Unauthorized', status: 401, contentType: 'text/plain' });
36+
return get401();
3737
}

src/routes/da-admin.js

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import putHelper from '../helpers/source.js';
1717
import { removeUEAttributes, unwrapParagraphs } from '../ue/attributes.js';
1818
import { prepareHtml } from '../ue/ue.js';
1919
import { getAemCtx, getAEMHtml } from '../utils/aemCtx.js';
20-
import { daResp } from '../responses/index.js';
20+
import { daResp, get401, get404 } from '../responses/index.js';
2121
import { BRANCH_NOT_FOUND_HTML_MESSAGE, DEFAULT_HTML_TEMPLATE, UNAUTHORIZED_HTML_MESSAGE } from '../utils/constants.js';
2222
import { getSiteConfig } from '../storage/config.js';
2323

@@ -67,25 +67,16 @@ export async function daSourceGet({ req, env, daCtx }) {
6767
org, site, path, ext, authToken,
6868
} = daCtx;
6969

70-
const response = {
71-
status: 200,
72-
contentType: 'text/html; charset=utf-8',
73-
};
74-
7570
// check if Authorization header is present
7671
if (!authToken) {
77-
response.body = UNAUTHORIZED_HTML_MESSAGE;
78-
response.status = 401;
79-
return daResp(response);
72+
return get401(UNAUTHORIZED_HTML_MESSAGE);
8073
}
8174

8275
// get the AEM parts (head.html)
8376
const aemCtx = getAemCtx(env, daCtx);
8477
const headHtml = await getAEMHtml(aemCtx, '/head.html');
8578
if (!headHtml) {
86-
response.body = BRANCH_NOT_FOUND_HTML_MESSAGE;
87-
response.status = 404;
88-
return daResp(response);
79+
return get404(BRANCH_NOT_FOUND_HTML_MESSAGE);
8980
}
9081

9182
// get the content from DA admin
@@ -102,21 +93,26 @@ export async function daSourceGet({ req, env, daCtx }) {
10293
method: 'GET',
10394
headers,
10495
});
96+
let body;
10597
const daAdminResp = await env.daadmin.fetch(req);
10698
if (daAdminResp && daAdminResp.status === 200) {
10799
// enrich stored content with HTML header and UE attributes
108100
const originalBodyHtml = await daAdminResp.text();
109101
const responseHtml = await prepareHtml(daCtx, aemCtx, originalBodyHtml, headHtml);
110-
response.body = responseHtml;
102+
body = responseHtml;
111103
} else {
112104
// enrich default template with HTML header and UE attributes
113105
const templateHtml = await getPageTemplate(env, daCtx, aemCtx, headHtml);
114106
const responseHtml = await prepareHtml(daCtx, aemCtx, templateHtml, headHtml);
115-
response.body = responseHtml;
107+
body = responseHtml;
116108
}
117109

118-
response.contentLength = response.body.length;
119-
return daResp(response);
110+
return daResp({
111+
status: 200,
112+
body,
113+
contentLength: body.length,
114+
contentType: 'text/html; charset=utf-8',
115+
});
120116
}
121117

122118
export async function daSourcePost({ req, env, daCtx }) {

src/utils/constants.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,5 @@ export const UNAUTHORIZED_HTML_MESSAGE = `
3131
export const DEFAULT_HTML_TEMPLATE = '<body><header></header><main><div></div></main><footer></footer></body>';
3232

3333
export const BRANCH_NOT_FOUND_HTML_MESSAGE = '<html><body><h1>Not found: Unable to retrieve AEM branch</h1></body></html>';
34+
35+
export const DEFAULT_UNAUTHORIZED_HTML_MESSAGE = '<html><body><h1>401: Unauthorized</h1></body></html>';

0 commit comments

Comments
 (0)