How to format errors to stdout in HAPI server? - exception

I need to reformat the console output from HAPI to spit out exceptions with the stack trace in a single line. Everything I come across looks like it's all about sending different info to the client, but I need to reformat the output going to stdout when an exception occurs.
I have tried adding a request-error handler and writing to console.log, and I see my formatted response, but the multi-line exception output still follows immediately afterward.
How do I format exception output?

Try doing console.log(new Error('asdf')) while watching stdout.
The stack trace should be included in that, but here's how to isolate it:
const test = new Error('test')
console.log(test.stack)
I'm not running node right now or I would confirm my recommendations.
In addition, I believe console.error() writes to stderr.
Read these:
Difference between "process.stdout.write" and "console.log" in node.js?
https://nodejs.org/api/console.html
You could probably do something like this:
const test = new Error('Detonations detected.')
console.log(`\n
TIME: ${new Date().valueOf()}
ERROR: ${test.message}
STACK: ${test.stack}
`)
// Try this exactly as shown first so you can see
// how the template literal is operating.
Is that kind of what you are looking for? Hopefully, that is illustrative enough.

Related

How do I make Drupal 9 treat exceptions like Drupal 7 does?

Without going into too many specifics, here is the problem.
I have a Drupal 7 site which includes an Ajax-called API that works something like this:
function myApi() {
// get data passed
try {
vendorFunctionCallUsingData();
} catch (Exception $e) {
// send error message back to user
}
// send success message back to user
}
...and it works perfectly. If the third-party code finds a problem with the data, it throws an exception, which is caught by my code and reported to the user.
I am converting this site to Drupal 9. Other than details like how the API is wrapped, it's identical to the D7 version: its code is exactly the same as the example above.
But for some reason, if the third-party code finds a problem with the data, Drupal catches the exception before my code can see it, logs it to watchdog, and returns a 500-coded White Screen Of Death (which is of no use to the calling JS, which is expecting a sensible result).
Question: how do I turn this behaviour off, so the exception reaches my try/catch? It's not the first time I've run up against Drupal 9 getting in the way of handling errors, but it is the first time I've been completely unable to code around the problem.

Reason for failure in MultipartObjectAssembler OCI object storage

I'm using MultipartObjectAssembler to upload data from a Database to OCI object storage.
Is there a way to know the reason for failure when using multipart upload?
When I try to commit assembler I'm getting IllegalStateException with the message "One or more parts were have not completed upload successfully".
I would like to know why any part got failed? I couldn't find a way to get this information from SDK.
try {
assembler.addPart(new ByteArrayInputStream(part, 0, length),
length,
null);
assembler.commit();
} catch (Exception e) {
assembler.abort();
throw new RuntimeException(e.getMessage());
}
Edit: I need to get an Exception thrown by a failed part and propagate the error message.
Is there a reason you are not using the UploadManager? It should do everything for you, including adding parts and committing. Here's an end-to-end example: https://github.com/oracle/oci-java-sdk/blob/master/bmc-examples/src/main/java/UploadObjectExample.java
If, for some reason, you cannot use UploadManager, please take a look at its source code nonetheless, since it demonstrates the intended usage of MultipartObjectAssembler: https://github.com/oracle/oci-java-sdk/blob/master/bmc-objectstorage/bmc-objectstorage-extensions/src/main/java/com/oracle/bmc/objectstorage/transfer/UploadManager.java#L175-L249
You create the MultipartObjectAssembler:
MultipartObjectAssembler assembler =
createAssembler(request, uploadRequest, executorServiceToUse);
You create a new request. This gives you back a MultipartManifest, which will later let you check if parts failed.
manifest =
assembler.newRequest(
request.getContentType(),
request.getContentLanguage(),
request.getContentEncoding(),
request.getOpcMeta());
Then you add all the parts:
assembler.addPart(
ProgressTrackingInputStreamFactory.create(
chunk, progressTrackerFactory.getProgressTracker()),
chunk.length(),
null);
Then you commit. This is where your code currently throws. I suspect not all parts have been added.
CommitMultipartUploadResponse response = assembler.commit();
If something goes wrong, check MultipartManifest.listCompletedParts(), MultipartManifest.listFailedParts(), and MultipartManifest.listInProgressParts(). The manifest should tell you what parts failed. Unfortunately not why; for that, you can enable ERROR level logging for com.oracle.bmc.objectstorage.transfer (for the class com.oracle.bmc.objectstorage.transfer.internal.MultipartTransferManager in particular).
If I have misunderstood something, please let me know. In that case, a larger, more complete code snippet would help me debug. Thanks!

youtube-dl: Failed to parse JSON

I posted a question on this yesterday to the GitHub support page and it got flagged as a duplicate - the original answer is here. This issue doesn't automatically fix like it did for that user. Instead, it seems to come and go with no pattern, so I don't have a good way to replicate it. Some songs will work at one point in time, then they won't a couple minutes later.
Error:
[debug] Encodings: locale cp1252, fs utf-8, out UTF-8, pref cp1252
[debug] youtube-dl version 2020.09.20
[debug] Python version 3.7.8 (CPython) - Windows-10-10.0.19041-SP0
[youtube:search] query "iron man 3 song": Downloading page 1
[debug] exe versions: none
[debug] Proxy map: {}
ERROR: query "song name": Failed to parse JSON caused by JSONDecodeError('Expecting value: line 1 column 1 (char 0)')); please report this issue on https://yt-dl.org/bug . Make sure you are using the latest version; see https://yt-dl.org/update on how to update. Be sure to call youtube-dl with the --verbose flag and include its complete output.
I get the issue when attempting to extract the data from the video. Here is a snippet of the code I am using:
ydlOps = {
'format': 'bestaudio/best',
'outtmpl': './%(title)s.webm',
'noplaylist': True,
'extractaudio': True,
'audioformat': 'webm',
'default_search': 'ytsearch1',
'quite': True,
'verbose':True,
'version': True
}
with youtube_dl.YoutubeDL(ydlOps) as downloader:
songData = downloader.extract_info(url, download=download)
I have changed the options, tried other options said to have worked, and nothing seems to make a difference. Some will work, then not, then they will again.
I think this is a youtube-dl bug. I wrote a parser for youtube searches and it also broke.
When looking previously at the response from youtube, all the JSON data was stored like this:
window["ytInitialData"] = {...}
So you just had to search through the server's response for the string 'window["ytInitialData"]' to find the relevant JSON and extract it. But now, youtube stores the JSON like this inside the html file sent by the server
var ytInitialData = {...}
This needs to be changed on youtube-dl's part when parsing the results.
What's strange is that sometimes youtube uses the previous version and sometimes it uses the current one. I think it's because the change in javascript is progressively being rolled out accross all youtube servers.
Also note that now, the line containing all the JSON ends with '; ' instead of just ';'. This might also require a change from youtube-dl.
You need to submit a pull request to youtube-dl or wait for somebody to fix it.

Meteor: reading simple JSON file

I am trying to read a JSON file with Meteor. I've seen various answers on stackoverflow but cannot seem to get them to work. I have tried this one which basically says:
Create a file called private/test.json with the following contents:
[{"id":1,"text":"foo"},{"id":2,"text":"bar"}]
Read the file contents when the server starts (server/start.js):
Meteor.startup(function() {
console.log(JSON.parse(Assets.getText('test.json')));
});
However this seemingly very simple example does not log anything to the console. If I trye to store it in a variable instead on console.logging it and then displaying it client side I get
Uncaught ReferenceError: myjson is not defined
where myjson was the variable I stored it in. I have tried reading the JSON client side
Template.hello.events({
'click input': function () {
myjson = JSON.parse(Assets.getText("myfile.json"));
console.log("myjson")
});
}
Which results in:
Uncaught ReferenceError: Assets is not defined
If have tried all of the options described here: Importing a JSON file in Meteor with more or less the same outcome.
Hope someone can help me out
As per the docs, Assets.getText is only available on the server as it's designed to read data in the private directory, to which clients should not have access (thus the name).
If you want to deliver this information to the client, you have two options:
Use Assets.getText exactly as you have done, but inside a method on the server, and call this method from the client to return the results. This seems like the best option to me as you're rationing access to your data via the method, rather than making it completely public.
Put it in the public folder instead and use something like jQuery.getJSON() to read it. This isn't something I've ever done, so I can't provide any further advice, but it looks pretty straightforward.
The server method is OK, just remove the extra semi-colon(;). You need a little more in the client call. The JSON data comes from the callback.
Use this in your click event:
if (typeof console !== 'undefined'){
console.log("You're calling readit");
Meteor.call('readit',function(err,response){
console.log(response);
});
}
Meteor!

ScriptDB: Traversing whole DB throws an exception. Am I doing somewthing wrong?

Playing with the ScriptDB Service, I created a new standalone script with the following function taken from the Documentation. The code throws the "Unexpected exception upon serializing continuation" exception in the var results = db.query({}); line. Am I doing something wrong or it is an issue to the GAS issue tracker?
function showAll() {
var db = ScriptDb.getMyDb();
var results = db.query({});
while (results.hasNext()) {
var result = results.next();
Logger.log(Utilities.jsonStringify(result));
}
}
Dont use the debugger: I had the same problem and read somewhere that the debugger produces this error. While I have not done much checking yet, I agree that it looks like a debugger issue.
Maybe star the issue: http://code.google.com/p/google-apps-script-issues/issues/detail?id=1267
Yes I looked into (googled) those continuation errors and it seems to be an implementation issue/limitation at Google's back end with the Rhino runtime Javascript environment being able to "suspend" when we as users of the IDE set breakpoints and step through code.
I now make sure I write debug lines into a spreadsheet. It can be slower to setup but it works 100% and is good engineering practice and it's usually faster to debug from logs than the slow, line by line stepping through of the code.