Monday, March 22, 2021

Sitecore SXA CLI: Theme Setup Error

 It is a copy of my article placed here to keep all my posts in one place.

A few days ago I was setting a new SXA Theme for the website. I used SXA CLI and followed official documentation, but faced with the error:

PS C:\Source\test> sxa new test
? The theme will be created for instance based on url http://localhost/. Do you want to specify a different url? no
? Enter your login sitecore\test
? Enter your password [hidden]
? Specify theme path. Root is Themes folder(format <RootFolderName/ChildFolderName>) CustomThemePath
? Do you want to set up theme config file? yes
C:\Users\Anton\AppData\Roaming\nvm\v10.15.1\node_modules\@sxa\CLI\util\serverRequests.js:26
                    _6bc‍.g.console.log(chalk.bold.red(body.match(/<li>([\d\w\s\.]*)<\/li>/)[1]))
                                                                                            ^

TypeError: Cannot read property '1' of null
    at Request.request [as _callback] (C:\Users\Anton\AppData\Roaming\nvm\v10.15.1\node_modules\@sxa\CLI\util\serverRequests.js:26:93)
    at Request.self.callback (C:\Users\Anton\AppData\Roaming\nvm\v10.15.1\node_modules\@sxa\CLI\node_modules\request\request.js:188:22)
    at Request.emit (events.js:189:13)
    at Request.EventEmitter.emit (domain.js:441:20)
    at Request.<anonymous> (C:\Users\Anton\AppData\Roaming\nvm\v10.15.1\node_modules\@sxa\CLI\node_modules\request\request.js:1171:10)
    at Request.emit (events.js:189:13)
    at Request.EventEmitter.emit (domain.js:441:20)
    at IncomingMessage.<anonymous> (C:\Users\Anton\AppData\Roaming\nvm\v10.15.1\node_modules\@sxa\CLI\node_modules\request\request.js:1091:12)
    at Object.onceWrapper (events.js:277:13)
    at IncomingMessage.emit (events.js:194:15)

Not too user-friendly error. Let's figure out what went wrong. First of all, we need to open SXA CLI NPM source and see what happens in serverRequests.js on line 26.

try {
    let response = JSON.parse(body);
    if (err) {
        console.log(chalk.red.bold('getting of Module list failed:' + err));
        return reject(err);
    }

    if (httpResponse.statusCode !== 200) {
        console.log(chalk.red.bold('Status code:' + httpResponse.statusCode));
        console.log(chalk.red.bold('Answer:' + httpResponse.body));
    }
    return resolve(response);
} catch (e) {
    if (typeof httpResponse == "undefined") {
        console.log(chalk.red.bold('Server did not provide any response'));
    } else {
        /* !!!!! This line is causing errror:  */
        console.log(chalk.bold.red(body.match(/<li>([\d\w\s\.]*)<\/li>/)[1]))
    }
    return reject(new Error('Error during parsing an answer from the server'))
}

Ok, SXA CLI got an exception and tries to parse the response body. Let's see what response do we get by adding console.log(body); to SXA CLI code. Now I have more information:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<title>404 - File or directory not found.</title>
</head>
<body>
<div id="header"><h1>Server Error</h1></div>
<div id="content">
 <div class="content-container"><fieldset>
  <h2>404 - File or directory not found.</h2>
  <h3>The resource you are looking for might have been removed, had its name changed, or is temporarily unavailable.</h3>
 </fieldset></div>
</div>
</body>
</html>

Now, we know that we get 404 on http://localhost/-/script/v2/master/Get-NewThemeModules request. And after further troubleshooting, we figure out that the user that is used with SXA CLI should have sitecore\PowerShell Extensions Remoting role. It is not enough to be the only administrator.

Conclusions:

  1. Write carefully code inside your catch blocks. It also could throw exceptions and make troubleshooting harder than it could be.
  2. User that is used for SXA CLI should have sitecore\PowerShell Extensions Remoting role.

No comments:

Post a Comment